Amazon web services AWS Cognito登录失败,Lambda函数中出现未知错误
我在lambda中使用了cognitonodejs sdk中的登录功能来验证用户,但在测试时它不起作用。 它在lambda控制台中响应未知错误 但是,在我静态网站上的代码中,IIt是有效的 请帮忙 这是我的nodejs lambda函数Amazon web services AWS Cognito登录失败,Lambda函数中出现未知错误,amazon-web-services,aws-lambda,cognito,Amazon Web Services,Aws Lambda,Cognito,我在lambda中使用了cognitonodejs sdk中的登录功能来验证用户,但在测试时它不起作用。 它在lambda控制台中响应未知错误 但是,在我静态网站上的代码中,IIt是有效的 请帮忙 这是我的nodejs lambda函数 global.fetch = require('node-fetch') var AmazonCognitoIdentity = require('amazon-cognito-identity-js'); /** params: userPoolId (
global.fetch = require('node-fetch')
var AmazonCognitoIdentity = require('amazon-cognito-identity-js');
/**
params:
userPoolId (string): aws cognito user pool id
clientId (string): aws cognito client id
username (string): aws cognito user username belonging to same userPool
password (string): aws cognito user password belonging to same userPool
**/
exports.handle = function(event, context, callback) {
//aliasing to make code readable
var CognitoUserPool = AmazonCognitoIdentity.CognitoUserPool;
var AuthenticationDetails = AmazonCognitoIdentity.AuthenticationDetails;
var CognitoUser = AmazonCognitoIdentity.CognitoUser;
var AuthenticationDetails = AmazonCognitoIdentity.AuthenticationDetails;
console.log(event);
//aws credentials
var poolData = {
UserPoolId : event.userPoolId,//'us-east-2_57jpQupfc', // Your user pool id here
ClientId : event.clientId//'7ec9h7hhtchermdsceg3v2p5ar' // Your client id here
};
var username = event.username;
var password = event.password;
var authenticationData = {
Username : username,
Password : password
};
var authenticationDetails = new AuthenticationDetails(authenticationData);
var userPool = new CognitoUserPool(poolData);
var userData = {
Username : username,
Pool : userPool
};
var cognitoUser = new CognitoUser(userData);
//sign in
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
var tokens = {};
var accessToken = result.getAccessToken().getJwtToken();
var idToken = result.getIdToken().getJwtToken();
tokens.accessToken = accessToken;
tokens.idToken = idToken;
/*
//POTENTIAL: Region needs to be set if not already set previously elsewhere.
AWS.config.region = '<region>';
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId : '...', // your identity pool id here
Logins : {
// Change the key below according to the specific region your user pool is in.
'cognito-idp.<region>.amazonaws.com/<YOUR_USER_POOL_ID>' : result.getIdToken().getJwtToken()
}
});
//refreshes credentials using AWS.CognitoIdentity.getCredentialsForIdentity()
AWS.config.credentials.refresh((error) => {
if (error) {
console.error(error);
} else {
// Instantiate aws sdk service objects now that the credentials have been updated.
// example: var s3 = new AWS.S3();
console.log('Successfully logged!');
}
});*/
callback(null, tokens);
},
onFailure: function(err) {
//callback(err.message || JSON.stringify(err));
callback(JSON.stringify(err), event);
},
});
}
这是我的webpack.config
var path = require('path');
module.exports = {
entry: './index.js',
// Place output files in `./dist/my-app.js`
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'my-app.js'
},
module: {
rules: [
{
test: /\.json$/,
loader: 'json-loader'
}
]
}
};
要回答为什么lambda报告错误,而它似乎在工作,是因为
回调的第一个参数不为null
回调(“hello12”,令牌)
将使Lambda报告错误,因为第一个参数是“hello12”
或不为空。请参阅回调的签名定义。尝试设置回调(空,令牌)
但是,您可能还希望将Lambda的回调放在refresh的函数回调中,如下所示
//refreshes credentials using AWS.CognitoIdentity.getCredentialsForIdentity()
AWS.config.credentials.refresh((error) => {
if (error) {
console.error(error);
callback(error); <-- here
} else {
console.log('Successfully logged!');
callback(null, tokens); // <-- and here
}
});*/
//使用AWS.CognitoIdentity.getCredentialsForIdentity()刷新凭据
AWS.config.credentials.refresh((错误)=>{
如果(错误){
控制台错误(error);
回调(错误);我按照你说的更改了回调,但错误是相同的。
//refreshes credentials using AWS.CognitoIdentity.getCredentialsForIdentity()
AWS.config.credentials.refresh((error) => {
if (error) {
console.error(error);
callback(error); <-- here
} else {
console.log('Successfully logged!');
callback(null, tokens); // <-- and here
}
});*/