Last active
February 10, 2021 07:16
-
-
Save y-o-u/ea85d20d7c05f42c7634ccee2bd78167 to your computer and use it in GitHub Desktop.
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
function login(email, password, callback) { | |
const AWS = require('aws-sdk@2.1.31'); | |
global.fetch = require('node-fetch'); | |
const AmazonCognitoIdentity = require('amazon-cognito-identity-js@3.0.14'); | |
const authenticationData = { | |
Username: email, | |
Password: password, | |
}; | |
const authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails( | |
authenticationData | |
); | |
const poolData = { | |
UserPoolId : configuration.userPoolId, | |
ClientId : configuration.clientId | |
}; | |
const userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData); | |
const userData = { | |
Username: email, | |
Pool: userPool, | |
}; | |
const cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData); | |
cognitoUser.authenticateUser(authenticationDetails, { | |
onSuccess(result){ | |
const token = result.idToken.jwtToken; | |
const pem = configuration.PEM.replace(/\\n/g, '\n'); | |
jwt.verify(token, pem, { algorithms: ['RS256'] }, function(err, decodedToken) { | |
if(err !== null){ | |
console.error(err); | |
}else{ | |
if(decodedToken.aud !== configuration.clientId) { | |
console.error(err); | |
return callback(err); | |
} | |
if(decodedToken.iss !== `https://cognito-idp.ap-northeast-1.amazonaws.com/${configuration.userPoolId}`) { | |
console.error(err); | |
return callback(err); | |
} | |
const nowTime = Math.floor(new Date().getTime() / 1000); | |
if(nowTime > decodedToken.exp) { | |
console.error(err); | |
return callback(err); | |
} | |
callback(null, { | |
//username: email, // Requires UsernameがONの時はいる | |
user_id: decodedToken.sub, | |
email: decodedToken.email | |
}); | |
} | |
}); | |
}, | |
onFailure(err){ | |
console.error(err); | |
}, | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment