Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import AWS from 'aws-sdk';
- import { CognitoUserPool, AuthenticationDetails, CognitoUser } from 'amazon-cognito-identity-js';
- export default function signinHandler(body) {
- const authData = JSON.parse(body);
- AWS.config.region = 'us-east-1';
- const authenticationDetails = {
- Username: authData.username,
- Password: authData.password,
- };
- const poolData = {
- UserPoolId: 'us-east-1_xxxxx',
- ClientId: 'xxxxxxxxxxxxxxxxxx'
- };
- const authDetails = new AuthenticationDetails(authenticationDetails);
- const userPool = new CognitoUserPool(poolData);
- const userData = {
- Username: authData.username,
- Pool: userPool,
- };
- const cognitoUser = new CognitoUser(userData);
- return new Promise((resolve, reject) => {
- cognitoUser.authenticateUser(authDetails, {
- onSuccess(result) {
- const response = {
- statusCode: 200,
- headers: {
- 'Content-Type': 'application/json',
- },
- body: JSON.stringify({
- token: result.getIdToken().getJwtToken(),
- authenticated: true,
- }),
- isBase64Encoded: false,
- };
- return resolve(response);
- },
- onFailure(error) {
- const response = {
- statusCode: 200,
- headers: {'Content-Type': 'application/json'},
- body: JSON.stringify({ error, authenticated: false }),
- isBase64Encoded: false,
- };
- return reject(response);
- },
- });
- });
- }
- exports.signin = async (event, context, callback) => {
- try {
- const response = await signinHandler(event.body);
- callback(null, response);
- } catch (error) {
- callback(error);
- }
- };
Add Comment
Please, Sign In to add comment