-
-
Save antstanley/c4d18c4fc9449a1dfe0106776b1c45c2 to your computer and use it in GitHub Desktop.
Get Cognito User Token
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
COGNITO_CLIENT_ID=<cognito client id> | |
COGNITO_USER_POOL_ID=<cognito user pool id> | |
COGNITO_USERNAME=<cognito username> | |
COGNITO_PASSWORD=<cognito user password> |
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 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