Skip to content

Instantly share code, notes, and snippets.

@antstanley
Created September 9, 2021 00:33
Show Gist options
  • Save antstanley/c4d18c4fc9449a1dfe0106776b1c45c2 to your computer and use it in GitHub Desktop.
Save antstanley/c4d18c4fc9449a1dfe0106776b1c45c2 to your computer and use it in GitHub Desktop.
Get Cognito User Token
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