Created
September 13, 2020 14:59
-
-
Save oxlb/cdea197cfa9fbb12b66a0eff7fe24284 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 jwt_decode = require('jwt-decode'); | |
const AmazonCognitoIdentity = require('amazon-cognito-identity-js'); | |
let cognitoAttributeList = []; | |
const poolData = { | |
UserPoolId : process.env.AWS_COGNITO_USER_POOL_ID, | |
ClientId : process.env.AWS_COGNITO_CLIENT_ID | |
}; | |
const attributes = (key, value) => { | |
return { | |
Name : key, | |
Value : value | |
} | |
}; | |
function setCognitoAttributeList(email, agent) { | |
let attributeList = []; | |
attributeList.push(attributes('email',email)); | |
attributeList.forEach(element => { | |
cognitoAttributeList.push(new AmazonCognitoIdentity.CognitoUserAttribute(element)); | |
}); | |
} | |
function getCognitoAttributeList() { | |
return cognitoAttributeList; | |
} | |
function getCognitoUser(email) { | |
const userData = { | |
Username: email, | |
Pool: getUserPool() | |
}; | |
return new AmazonCognitoIdentity.CognitoUser(userData); | |
} | |
function getUserPool(){ | |
return new AmazonCognitoIdentity.CognitoUserPool(poolData); | |
} | |
function getAuthDetails(email, password) { | |
var authenticationData = { | |
Username: email, | |
Password: password, | |
}; | |
return new AmazonCognitoIdentity.AuthenticationDetails(authenticationData); | |
} | |
function initAWS (region = process.env.AWS_COGNITO_REGION, identityPoolId = process.env.AWS_COGNITO_IDENTITY_POOL_ID) { | |
AWS.config.region = region; // Region | |
AWS.config.credentials = new AWS.CognitoIdentityCredentials({ | |
IdentityPoolId: identityPoolId, | |
}); | |
} | |
function decodeJWTToken(token) { | |
const { email, exp, auth_time , token_use, sub} = jwt_decode(token.idToken); | |
return { token, email, exp, uid: sub, auth_time, token_use }; | |
} | |
module.exports = { | |
initAWS, | |
getCognitoAttributeList, | |
getUserPool, | |
getCognitoUser, | |
setCognitoAttributeList, | |
getAuthDetails, | |
decodeJWTToken, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment