Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const custom_header = async () => {
try {
return { Authorization: (await Auth.currentSession()).getIdToken().getJwtToken() }
}
catch (e) {
console.warn(e, "Defaulting to stored JWT in localStorage...");
// Get JWT from SAML.
return { Authorization: await asyncLocalStorage.getItem("jwt") }
}
}
Auth.configure({
}
else {
Auth.signOut();
let attributes: IUserAttributes = { "name": parsed["name"], "email": parsed["email"], "email_verified": parsed["email_verified"], "cognito:groups": parsed["cognito:groups"] };
return await {
"username": parsed["sub"],
attributes
};
}
}
// If JWT from SAML has expired, or if there is no JWT in the first place, run this code.
// Need to parse our local JWT as well to get cognito:groups attribute, because Auth.currentAuthenticatedUser() does not return user groups.
return Promise.all([
Auth.currentAuthenticatedUser(),
parseJwt((await Auth.currentSession()).getIdToken().getJwtToken())
]).then(([user, token]) => {
user.attributes["cognito:groups"] = token["cognito:groups"];
return user;
});
}
async componentDidMount() {
try {
if (await Auth.currentSession()) {
this.userHasAuthenticated(true);
}
}
catch (e) {
if (e !== 'No current user') {
alert(e);
}
}
this.setState({ isAuthenticating: false });
}
if (!credentialsOK) {
throw new Error('No credentials');
}
break;
case 'OPENID_CONNECT':
const federatedInfo = await Cache.getItem('federatedInfo');
if (!federatedInfo || !federatedInfo.token) {
throw new Error('No federated jwt');
}
headers = {
Authorization: federatedInfo.token,
};
break;
case 'AMAZON_COGNITO_USER_POOLS':
const session = await Auth.currentSession();
headers = {
Authorization: session.getAccessToken().getJwtToken(),
};
break;
default:
headers = {
Authorization: null,
};
break;
}
return headers;
}
const getAuthToken = async () => {
try {
const session = await Auth.currentSession()
return session.getIdToken().getJwtToken()
} catch (e) {
return ''
}
}
jwtToken: async () => (
await Auth.currentSession()).getAccessToken().getJwtToken(),
},
if (!credentialsOK) {
throw new Error('No credentials');
}
break;
case 'OPENID_CONNECT':
const federatedInfo = await Cache.getItem('federatedInfo');
if (!federatedInfo || !federatedInfo.token) {
throw new Error('No federated jwt');
}
headers = {
Authorization: federatedInfo.token,
};
break;
case 'AMAZON_COGNITO_USER_POOLS':
const session = await Auth.currentSession();
headers = {
Authorization: session.getAccessToken().getJwtToken(),
};
break;
default:
headers = {
Authorization: null,
};
break;
}
return headers;
}
const getIdToken = async () => {
const cognitoUser = await Auth.currentSession();
return cognitoUser ? cognitoUser.idToken.jwtToken : null;
};