Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const ACI = require("amazon-cognito-identity-js");
- const AWS = require("aws-sdk");
- exports.handler = (event, context, callback) => {
- var authenticationData = {
- Username : event.username ? event.username : "",
- Password : event.password ? event.password : "",
- };
- var authenticationDetails = new ACI.AuthenticationDetails(authenticationData);
- var poolData = {
- UserPoolId : 'eu-west-2_hwI4lvY1T', // Your user pool id here
- ClientId : '4e20kvmotvvbgc79srv3u682fn' // Your client id here
- };
- var userPool = new ACI.CognitoUserPool(poolData);
- var userData = {
- Username : event.username ? event.username : "",
- Pool : userPool
- };
- var cognitoUser = new ACI.CognitoUser(userData);
- cognitoUser.authenticateUser(authenticationDetails, {
- onSuccess: function (result) {
- var accessToken = result.getIdToken().getJwtToken();
- callback(accessToken);
- return;
- var attributeList = [];
- var attribute = {
- Name : 'custom:x-api-key',
- Value : 'DYGbjx1IEc4xAwK4oOQN959SfrJDxkXKaFvjBO1S'
- };
- var attribute = new ACI.CognitoUserAttribute(attribute);
- attributeList.push(attribute);
- cognitoUser.updateAttributes(attributeList, function(err, result) {
- if (err) {
- console.log("ERR" + err.message || JSON.stringify(err));
- return;
- }
- console.log('call result: ' + result);
- });
- return;
- // Initialize the Amazon Cognito credentials provider
- AWS.config.region = 'eu-west-2'; // Region
- AWS.config.credentials = new AWS.CognitoIdentityCredentials({
- IdentityPoolId: 'eu-west-2:2bc384fb-f85a-46bd-81ee-b3b6d86834bc',
- Logins: {
- 'cognito-idp.eu-west-2.amazonaws.com/eu-west-2_hwI4lvY1T': accessToken
- }
- });
- AWS.config.getCredentials((error) => {
- if (error) {
- callback("Error: " + error.message || JSON.stringify(error), null);
- } else {
- callback(null, AWS.config.credentials.data);
- }
- });
- },
- onFailure: function(err) {
- callback("Error: " + err.message || JSON.stringify(err), null);
- },
- });
- };
- exports.handler({username: "xAdlerTest2", password: "password"}, {}, callbacks);
- function callbacks(s, a) {
- console.log(s || a);
- }
Add Comment
Please, Sign In to add comment