Advertisement
Guest User

Untitled

a guest
Apr 7th, 2018
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var express = require('express');
  2. var router = express.Router();
  3.  
  4. var AWS = require('aws-sdk');
  5. var AmazonCognitoIdentity = require('amazon-cognito-identity-js');
  6. var CognitoUserPool = AmazonCognitoIdentity.CognitoUserPool;
  7.  
  8.  
  9. /* GET login page. */
  10. router.get('/', function(req, res, next) {
  11.   res.render('index', { title: 'SecureFTP' });
  12. });
  13.  
  14. router.post('/', function(req, res){
  15.    
  16.     //Gather username and password fields
  17.     var username = req.body.username;
  18.     var password = req.body.password;
  19.    
  20.     var authenticationData = {
  21.         Username : username,
  22.         Password : password,
  23.     };
  24.     var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
  25.     var poolData = {
  26.         UserPoolId : 'removed for security purposes', // Your user pool id here
  27.         ClientId : 'removed for security purposes' // Your client id here
  28.     };
  29.     var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
  30.     var userData = {
  31.         Username : username,
  32.         Pool : userPool
  33.     };
  34.     var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
  35.     cognitoUser.authenticateUser(authenticationDetails, {
  36.         onSuccess: function (result) {
  37.             AWS.config.region = 'us-west-2';
  38.  
  39.             AWS.config.credentials = new AWS.CognitoIdentityCredentials({
  40.                 IdentityPoolId : 'removed for security purposes', // your identity pool id here
  41.                 Logins : {
  42.                     // Change the key below according to the specific region your user pool is in.
  43.                     'removed for security purposes' : result.getIdToken().getJwtToken()
  44.                 }
  45.             });
  46.  
  47.             //refreshes credentials using AWS.CognitoIdentity.getCredentialsForIdentity()
  48.             AWS.config.credentials.refresh((error) => {
  49.                 if (error) {
  50.                      console.error(error);
  51.                 } else {
  52.                      // Instantiate aws sdk service objects now that the credentials have been updated.
  53.                      // example: var s3 = new AWS.S3();
  54.                      console.log('Successfully logged!');
  55.                      res.redirect('/folders');
  56.                 }
  57.             });
  58.         },
  59.        
  60.         newPasswordRequired: function(userAttributes, requiredAttributes) {
  61.             delete userAttributes.email_verified;
  62.             //res.redirect('/changepass');
  63.             //var newpass = req.body.new_password;
  64.             //This newpass variable would take the place of "hardcodedpassword" below
  65.             cognitoUser.completeNewPasswordChallenge("hardcodedpassword", userAttributes, this);
  66.             //User then needs to be redirected to another page
  67.  
  68.         },
  69.  
  70.         onFailure: function(err) {
  71.             console.log(err);
  72.             console.log(new Error().stack);
  73.         },
  74.  
  75.     });
  76.  
  77. });
  78.  
  79. module.exports = router;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement