-
-
Save xeusteerapat/9f820b2fabb77a2328427a52d97c54f9 to your computer and use it in GitHub Desktop.
Get AWS Cognito Token ID (JWT) with JavaScript (NodeJS)
This file contains 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
const AWS = require('aws-sdk'); | |
const CognitoSDK = require('amazon-cognito-identity-js-node'); | |
AWS.CognitoIdentityServiceProvider.AuthenticationDetails = CognitoSDK.AuthenticationDetails; | |
AWS.CognitoIdentityServiceProvider.CognitoUserPool = CognitoSDK.CognitoUserPool; | |
AWS.CognitoIdentityServiceProvider.CognitoUser = CognitoSDK.CognitoUser; | |
const Username = 'testuser'; | |
const TempPassword = 'TemporaryPassword2!'; | |
const NewPassword = 'NewPassword@#@!19'; | |
const Email = 'some@email.com'; | |
const config = { region: 'us-east-1' }; | |
const UserPoolId = 'USER_POOL_ID_HERE'; | |
const ClientId = 'APP_CLIENT_ID_HERE'; // Your App client id (add via Console->Cognito User Pool) | |
const cognitoIdentityServiceProvider = | |
new AWS.CognitoIdentityServiceProvider(config); | |
const saveOrUpdateUser = (profile) => { | |
//User Pool | |
const poolData = { | |
UserPoolId : UserPoolId, | |
ClientId : ClientId // Your App client id here | |
}; | |
const userPool = new AWS.CognitoIdentityServiceProvider.CognitoUserPool(poolData); | |
//User | |
const userParams = { | |
Pool: userPool, | |
Username: Username, | |
}; | |
var cognitoUser = new AWS.CognitoIdentityServiceProvider.CognitoUser(userParams); | |
//Authentication | |
const authenticationData = { | |
Username: Username, | |
Password: NewPassword, //1st time use TempPassword | |
} | |
const authenticationDetails = new AWS.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData); | |
var responseFunctions = { | |
onSuccess: (result) => { | |
console.log('IT WORKED!'); | |
console.log(result); | |
}, | |
onFailure: (err) => { | |
console.log('no go :('); | |
console.log(err); | |
} | |
}; | |
//newPasswordRequired has to be added separately because it sends responseFunctions to completeNewPasswordChallenge | |
responseFunctions.newPasswordRequired = (userAttributes, requiredAttributes) => { | |
delete userAttributes.email_verified; | |
cognitoUser.completeNewPasswordChallenge(NewPassword, {email: Email}, responseFunctions) | |
}; | |
cognitoUser.authenticateUser(authenticationDetails, responseFunctions); | |
}; | |
saveOrUpdateUser(); |
This file contains 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
{ | |
"name": "test-app", | |
"description": "test app", | |
"version": "0.0.1", | |
"engines": { | |
"node": ">=6.3.1" | |
}, | |
"devDependencies": { | |
"amazon-cognito-identity-js-node": "0.0.3", | |
"aws-sdk": "^2.5.3" | |
} | |
} |
This file contains 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
events: | |
- http: | |
path: restricted | |
method: get | |
cors: true | |
integration: lambda | |
authorizer: | |
arn: arn:aws:cognito-idp:AWS_REGION:AWS_ACCOUNT_ID:userpool/AWS_USERPOOL_ID | |
resultTtlInSeconds: 0 | |
claims: | |
identitySource: method.request.header.Authorization | |
identityValidationExpression: .* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment