Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require('express');
- var router = express.Router();
- var AWS = require('aws-sdk');
- var AmazonCognitoIdentity = require('amazon-cognito-identity-js');
- var CognitoUserPool = AmazonCognitoIdentity.CognitoUserPool;
- /* GET login page. */
- router.get('/', function(req, res, next) {
- res.render('index', { title: 'SecureFTP' });
- });
- router.post('/', function(req, res){
- //Gather username and password fields
- var username = req.body.username;
- var password = req.body.password;
- var authenticationData = {
- Username : username,
- Password : password,
- };
- var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
- var poolData = {
- UserPoolId : 'removed for security purposes', // Your user pool id here
- ClientId : 'removed for security purposes' // Your client id here
- };
- var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
- var userData = {
- Username : username,
- Pool : userPool
- };
- var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
- cognitoUser.authenticateUser(authenticationDetails, {
- onSuccess: function (result) {
- AWS.config.region = 'us-west-2';
- AWS.config.credentials = new AWS.CognitoIdentityCredentials({
- IdentityPoolId : 'removed for security purposes', // your identity pool id here
- Logins : {
- // Change the key below according to the specific region your user pool is in.
- 'removed for security purposes' : 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!');
- res.redirect('/folders');
- }
- });
- },
- newPasswordRequired: function(userAttributes, requiredAttributes) {
- delete userAttributes.email_verified;
- //res.redirect('/changepass');
- //var newpass = req.body.new_password;
- //This newpass variable would take the place of "hardcodedpassword" below
- cognitoUser.completeNewPasswordChallenge("hardcodedpassword", userAttributes, this);
- //User then needs to be redirected to another page
- },
- onFailure: function(err) {
- console.log(err);
- console.log(new Error().stack);
- },
- });
- });
- module.exports = router;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement