Я использовал функцию входа из cognito nodejs sdk в моей лямбде для аутентификации пользователя, и она не работает, когда я тестирую ее.Это отвечает Неизвестной ошибкой в лямбда-консоли.
Однако это работает в моем коде на моем статическом веб-сайте.
Пожалуйста, помогите.
Это моя лямбда-функция nodejs.
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);
},
});
}
Это мой package.json
{
"name": "sign-in",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"json-loader": "^0.5.7",
"webpack": "^4.8.1",
"webpack-cli": "^2.1.3"
},
"dependencies": {
"amazon-cognito-identity-js": "^2.0.3"
}
}
Это мой веб-пакет.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'
}
]
}
};