Skip to content

Instantly share code, notes, and snippets.

@franzwong
Created January 22, 2019 11:57
Show Gist options
  • Save franzwong/0f4d4747e42ddce11c3a6f961ec27627 to your computer and use it in GitHub Desktop.
Save franzwong/0f4d4747e42ddce11c3a6f961ec27627 to your computer and use it in GitHub Desktop.
HowTo: Implement user sign up and login with AWS Cognito
async function signUp(email, password) {
try {
const cognito = new AWS.CognitoIdentityServiceProvider()
await cognito.adminCreateUser({
UserPoolId: process.env.USER_POOL_ID,
Username: email,
MessageAction: 'SUPPRESS',
TemporaryPassword: password,
}).promise()
const initAuthResponse = await cognito.adminInitiateAuth({
AuthFlow: 'ADMIN_NO_SRP_AUTH',
ClientId: process.env.CLIENT_ID,
UserPoolId: process.env.USER_POOL_ID,
AuthParameters: {
USERNAME: email,
PASSWORD: password
}
}).promise()
if (initAuthResponse.ChallengeName === 'NEW_PASSWORD_REQUIRED') {
await cognito.adminRespondToAuthChallenge({
ChallengeName: 'NEW_PASSWORD_REQUIRED',
ClientId: process.env.CLIENT_ID,
UserPoolId: process.env.USER_POOL_ID,
ChallengeResponses: {
USERNAME: email,
NEW_PASSWORD: password,
},
Session: initAuthResponse.Session
}).promise()
}
} catch (err) {
throw err
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment