I have a lambda that creates the tokens for an existing user in my user pool, but when I am to validate the access token returns an error 401 and when I try with ID token it returns a 200.
function asyncAuthenticateUser(cognitoUser, cognitoAuthenticationDetails) {
return new Promise(function(resolve, reject) {
cognitoUser.authenticateUser(cognitoAuthenticationDetails, {
onSuccess: resolve,
onFailure: reject
});
});
}
var authenticationData = {
Username: name,
Password: password,
};
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
var poolData = {
UserPoolId: UserPoolId,
ClientId: ClientId
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var userData = {
Username: name,
Pool: userPool
};
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
try {
let session = await asyncAuthenticateUser(cognitoUser, authenticationDetails);
cognitoJWT.session = session;
console.log(session.getIdToken());
cognitoJWT.jwtAccess = session.getAccessToken().getJwtToken();
cognitoJWT.jwtId = session.getIdToken().getJwtToken();
cognitoJWT.jwtRefresh = session.getRefreshToken().getToken();
cognitoJWT.jwtPayloads = {
jwtAccess: session.getAccessToken().decodePayload(),
jwtId: session.getIdToken().decodePayload(),
};
callback(null, cognitoJWT);
} catch (err){callback(err,null); }
My output of the Lambda is:
{
"StatusCode": 200,
"StatusMessage": {
"jwtAccess": "<<jwtAccess>>",
"jwtRefresh":"<<jwtRefresh>>",
"jwtId":<<jwtId>>
}
}
But when I am trying to validate on the API gateway I got this output.
If I am Try the jwtID it shows the user info. Output API gateway jwtId