Skip to content

Instantly share code, notes, and snippets.

@franzwong
Created January 23, 2019 14:29
Show Gist options
  • Save franzwong/7035a80e51af5acd55084ae20d19c9ca to your computer and use it in GitHub Desktop.
Save franzwong/7035a80e51af5acd55084ae20d19c9ca to your computer and use it in GitHub Desktop.
HowTo: Implement user sign up and login with AWS Cognito
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