对整个EC2实例进行身份验证的Cognito……看来呢?

时间:2019-10-21 04:32:53

标签: node.js amazon-ec2 amazon-cognito

似乎当我登录到我的应用程序时,它正在将凭据本地存储在EC2服务器上。我已经四倍地检查了我的代码,但我不知道这是什么交易。如果KEVIN从任何设备登录,则下一个用户登录,或者如果某人刷新其页面,则最终以KEVIN登录。我包括了可能与该问题有关的所有代码。除此问题外,我与cognito的所有交互都非常有效;没有错误,没有问题。任何帮助将不胜感激。我在EC2实例上使用Node.js,Express,AWS和Websocket。

// Accessed from server for route authentication before page render or redirection

const AmazonCognitoIdentity = require('amazon-cognito-identity-js');

module.exports = {
    ensureAuthenticated: function(req, res, next) {
        let data = { UserPoolId : 'us-east-1_7xUGRJPKq',
        ClientId : '6glign6b34c806osfhnik18cb3'
        };
        let userPool = new AmazonCognitoIdentity.CognitoUserPool(data);
        let cognitoUser = userPool.getCurrentUser();
        console.log(`ensuring authentication....`);
        console.log(cognitoUser);
        if (cognitoUser === null) {
            req.flash('error_msg', 'Please log in');
            res.redirect('/login');
        } else {
            cognitoUser.getSession((err, session) => {
                if (err) {
                    console.log(err);
                } else {
                    next();
                }
            }); 
        }
    },
};

// Routes where I am seeing the problem

const express = require('express');
const router = express.Router();
const AmazonCognitoIdentity = require('amazon-cognito-identity-js');
const { ensureAuthenticated } = require('../config/auth.js');

router.get('/', (req, res) => {
    res.redirect('/dashboard');
});

router.get('/dashboard', ensureAuthenticated, (req, res) => {
    res.render('dashboard', {
        layout: './layouts/dashboard-layout.ejs'
    });
});

// Login authentication

router.post('/login', (req, res) => {
    const loginDetails = {
        Username: req.body.email,
        Password: req.body.password
    }
    const authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(loginDetails);

    const userDetails = {
        Username: req.body.email,
        Pool: userPool
    }
    const cognitoUser = new AmazonCognitoIdentity.CognitoUser(userDetails);

    cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: data => {
            cognitoUser.getUserAttributes(function(err, result) {
                if (err) {
                    console.log(err);
                    return;
                } else {
                    console.log('LOGIN RESULTS')
                    console.log(result[0].Value.toString());
                    let userId = result[0].Value.toString();
                    let userCity = result[2].Value.toString();
                    console.log(userId);
                    console.log(userCity);
                    res.redirect(`/dashboard/${userCity}/${userId}/`)
                };
            });
        },
        onFailure: err => {
            req.flash('error_msg', 'Invalid Credentials');
            console.error(err);
            res.redirect('/login');
        }
    })
});

谢谢!

UPDATE 10/21/19:删除了不适用于该问题的功能。

在本地存储中似乎也没有任何JWT: Click Here for image

0 个答案:

没有答案