I am using aws-amplify
v6 inside my react-native app.
Authentication functionality is working correctly however I could not access raw access/id tokens after login.
Trying to retrieve the tokens like:
import { fetchAuthSession } from "aws-amplify/auth";
...
async getCurrentSession() {
const session = await fetchAuthSession();
console.log("Token: ", JSON.stringify(session.tokens?.idToken));
}
However, tokens inside the session are of the type of JwtPayloadStandardFields
and contain only parsed payloads of the tokens i.e.
{
"payload":{
"sub":"4ac665ba-2e3c-40a0-93d8-515e2224fe22",
"email_verified":true,
"is":"...",
"cognito:username":"...",
"origin_jti":"c9f42d85-b7f2-4780-995a-562ff120d6c7",
"aud":"...",
"event_id":"2fe27904-8de0-4b53-80b5-abe166f98e9f",
"token_use":"id",
"auth_time":1701865836,
"exp":1702047957,
"iat":1702044358,
"jti":"28516f62-0cab-4a5d-a6f1-eb6dff6cadee",
"email":"xxx"
}
}
I am wondering how to access actual raw tokens as I need them to pass to API clients.
2
Answers
This is how I am using it in Angular TypeScript with Amplify v6.
Import
fetchAuthSession
on top.Then, just apply
.toString()
method onidToken
property.As you can see in the below screenshot, it offers 2 properties.
Here is a screenshot that I captured during debugging.
Another option is to use
cognitoUserPoolsTokenProvider
after you config token-saving properlyIt can use like this.
I am using "aws-amplify": "^6.0.10"