我的Cognito代码按意向返回令牌,或者在凭据错误的情况下返回错误,但是为了方便读取/引用,我将其分为不同的模块。
但是,这不会按预期方式流动令牌。我知道我做错了事,但似乎无法弄清我要去哪里。如果我将整个认知代码都移到api调用上,那么它将起作用。
存在认知功能的模块
var authenticate = (val) => {
var acc;
console.log(val);
var userData = {
Username: val.user, // your username here
Pool: userPool
};
var authenticationData = {
Username: val.user, // your username here
Password: val.pass, // your password here
};
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function(result) {
var accessToken = result.getAccessToken().getJwtToken();
console.log(`access token is ${accessToken}`);
return accessToken;
},
onFailure: function(err) {
console.log(err);
return err;
}
});
}
module.exports = {
authenticate
};
我需要“身份验证”并传递值的模块
router.post('/api/auth', (req, res) => {
var val = req.body;
console.log(val.value);
var x = authenticate(val.value);
res.json(x);
})