Created
January 23, 2019 14:29
-
-
Save franzwong/7035a80e51af5acd55084ae20d19c9ca to your computer and use it in GitHub Desktop.
HowTo: Implement user sign up and login with AWS Cognito
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function getUserCredentials(idToken) { | |
const cognitoidentity = new AWS.CognitoIdentity() | |
const providerName = `cognito-idp.${process.env.AWS_REGION}.amazonaws.com/${process.env.USER_POOL_ID}` | |
let response = await cognitoidentity.getId({ | |
IdentityPoolId: process.env.IDENTITY_POOL_ID, | |
Logins: { | |
[providerName]: idToken | |
} | |
}).promise() | |
const identityId = response.IdentityId | |
response = await cognitoidentity.getCredentialsForIdentity({ | |
IdentityId: identityId, | |
Logins: { | |
[providerName]: idToken | |
} | |
}).promise() | |
const credentials = new AWS.Credentials( | |
response.Credentials.AccessKeyId, | |
response.Credentials.SecretKey, | |
response.Credentials.SessionToken | |
) | |
return { | |
identityId, | |
credentials | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment