46

I'm trying to figure out how to access the accessToken, refreshToken, and idToken that I receive back from aws-amplify using the Auth library.

example in docs: https://aws.github.io/aws-amplify/media/authentication_guide.html

example of my usage:

const user = await Auth.signIn(email, password);

user has a bunch of properties that are inaccessible including everything I need. In the docs, it's unclear how to get to these properties because the examples all log the result. Any ideas?

spencewine
  • 470
  • 1
  • 4
  • 7
  • 3
    I posted this question in the aws-amplify repo and a collaborator replied: Auth.currentSession().accessToken.jwtToken – spencewine Feb 15 '18 at 17:11

6 Answers6

55
Auth.currentSession().then(res=>{
  let accessToken = res.getAccessToken()
  let jwt = accessToken.getJwtToken()
      
  //You can print them to see the full objects
  console.log(`myAccessToken: ${JSON.stringify(accessToken)}`)
  console.log(`myJwt: ${jwt}`)
})
André Kuhlmann
  • 4,378
  • 3
  • 23
  • 42
Omar
  • 1,005
  • 11
  • 11
22

Auth.currentSession() will return a CognitoUserSession containing accessToken, idToken, and refreshToken.

The CognitoUserSession is actually the following: CognitoUserSession {idToken: CognitoIdToken, refreshToken: CognitoRefreshToken, accessToken: CognitoAccessToken, clockDrift: 0}

Accessing pairs within that object can be achieved through straightforward dot notation at this point.


Example: Retrieve the accessToken and log to console

Auth.currentSession().then(data => console.log(data.accessToken));

The result will be a CognitoAccessToken in the form CognitoAccessToken { jwtToken: '', payload: ''}

If you just want the jwtToken within the CognitoAccessToken, it's just dot notation all the way down (with log to console example):

Auth.currentSession().then(data => console.log(data.accessToken.jwtToken));

Note: This method also refreshes the current session if needed (reference).

xrpza
  • 329
  • 3
  • 6
4

I believe you can do

Auth.currentCredentials(credentials => {
  const tokens = Auth.essentialCredentials(credentials);
})

where essentialCredentials will return all of the tokens

Hope this helps.

iurii
  • 4,142
  • 2
  • 23
  • 28
4

Angular 9, getting JWT token from current session :

import Auth from '@aws-amplify/auth';

Auth.currentSession().then(data => console.log("JWT", data.getAccessToken().getJwtToken()));
Vladyslav Didenko
  • 1,352
  • 1
  • 14
  • 19
1

For those in search of the AWSCredentials:

const checkCognitoUserSession = async () => {
  const getAwsCredentials = await Auth.currentCredentials();
  const awsCredentials = await Auth.essentialCredentials(getAwsCredentials);

  // accessKeyId, secretAccessKey, sessionToken post login
  return { awsCredentials };
};
O.O
  • 1,389
  • 14
  • 29
1

Retrieve current session using aws-amplify

Auth.currentSession() returns a CognitoUserSession object which contains JWT accessToken, idToken, and refreshToken.

Auth.currentSession()
.then((data) => console.log(data))
.catch((err) => console.log(err));

aws-amplify Docs currentSession

Irushan
  • 125
  • 2
  • 10