Skip to content

Instantly share code, notes, and snippets.

@jrothman
Forked from antstanley/.env
Last active September 13, 2021 16:44
Show Gist options
  • Save jrothman/f9b12cbfc2a8f910b756a5958ea2a0a7 to your computer and use it in GitHub Desktop.
Save jrothman/f9b12cbfc2a8f910b756a5958ea2a0a7 to your computer and use it in GitHub Desktop.
Get Cognito User Token
// get cognito token on demand
COGNITO_CLIENT_ID=<cognito client id>
COGNITO_USER_POOL_ID=<cognito user pool id>
COGNITO_USERNAME=<cognito username>
COGNITO_PASSWORD=<cognito user password>
const AWS = require('aws-sdk')
const clientId = process.env.COGNITO_CLIENT_ID
const userPoolId = process.env.COGNITO_USER_POOL_ID
const username = process.env.COGNITO_USERNAME
const password = process.env.COGNITO_PASSWORD
const cognito = new AWS.CognitoIdentityServiceProvider({
apiVersion: '2016-04-18'
})
const getToken = async authParams => {
let result = {}
try {
const params = {
AuthFlow: 'ADMIN_USER_PASSWORD_AUTH',
ClientId: clientId,
UserPoolId: userPoolId,
AuthParameters: {
USERNAME: username,
PASSWORD: password
}
}
const authRequest = await cognito.adminInitiateAuth(params).promise()
result = authRequest.AuthenticationResult
} catch (error) {
console.error('getCognitoToken', error)
}
return result
}
module.exports = getToken
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment