Guest User

Untitled

a guest
Dec 29th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. import AWS from 'aws-sdk';
  2. import { CognitoUserPool, AuthenticationDetails, CognitoUser } from 'amazon-cognito-identity-js';
  3.  
  4. export default function signinHandler(body) {
  5. const authData = JSON.parse(body);
  6. AWS.config.region = 'us-east-1';
  7. const authenticationDetails = {
  8. Username: authData.username,
  9. Password: authData.password,
  10. };
  11.  
  12. const poolData = {
  13. UserPoolId: 'us-east-1_xxxxx',
  14. ClientId: 'xxxxxxxxxxxxxxxxxx'
  15. };
  16.  
  17. const authDetails = new AuthenticationDetails(authenticationDetails);
  18. const userPool = new CognitoUserPool(poolData);
  19. const userData = {
  20. Username: authData.username,
  21. Pool: userPool,
  22. };
  23. const cognitoUser = new CognitoUser(userData);
  24.  
  25. return new Promise((resolve, reject) => {
  26. cognitoUser.authenticateUser(authDetails, {
  27. onSuccess(result) {
  28. const response = {
  29. statusCode: 200,
  30. headers: {
  31. 'Content-Type': 'application/json',
  32. },
  33. body: JSON.stringify({
  34. token: result.getIdToken().getJwtToken(),
  35. authenticated: true,
  36. }),
  37. isBase64Encoded: false,
  38. };
  39. return resolve(response);
  40. },
  41.  
  42. onFailure(error) {
  43. const response = {
  44. statusCode: 200,
  45. headers: {'Content-Type': 'application/json'},
  46. body: JSON.stringify({ error, authenticated: false }),
  47. isBase64Encoded: false,
  48. };
  49. return reject(response);
  50. },
  51. });
  52. });
  53. }
  54.  
  55. exports.signin = async (event, context, callback) => {
  56. try {
  57. const response = await signinHandler(event.body);
  58. callback(null, response);
  59. } catch (error) {
  60. callback(error);
  61. }
  62. };
Add Comment
Please, Sign In to add comment