Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AWS Amplify/Cognito:如何正确检查用户是否经过身份验证?_Javascript_Api_Express_Amazon Cognito_Aws Amplify - Fatal编程技术网

Javascript AWS Amplify/Cognito:如何正确检查用户是否经过身份验证?

Javascript AWS Amplify/Cognito:如何正确检查用户是否经过身份验证?,javascript,api,express,amazon-cognito,aws-amplify,Javascript,Api,Express,Amazon Cognito,Aws Amplify,我最近在我的网站上切换到Cognito进行身份验证。我正在使用NodeJS/Express运行服务器。由于每个API调用在声明和主函数之间都有一个回调,用于检查用户是否经过身份验证,currentAuthenticatedUser(),因此应该为当前的req/会话返回一个cognito用户对象 我发现,在有人登录之前,这个函数可以正常工作。如果我像平常一样登录,我的帐户就会通过身份验证,我可以访问主页等 但是,如果我向其他人发送网站链接,他们会自动绕过登录,因为此函数返回我的用户对象,而不是按请

我最近在我的网站上切换到Cognito进行身份验证。我正在使用NodeJS/Express运行服务器。由于每个API调用在声明和主函数之间都有一个回调,用于检查用户是否经过身份验证,
currentAuthenticatedUser()
,因此应该为当前的req/会话返回一个cognito用户对象

我发现,在有人登录之前,这个函数可以正常工作。如果我像平常一样登录,我的帐户就会通过身份验证,我可以访问主页等

但是,如果我向其他人发送网站链接,他们会自动绕过登录,因为此函数返回我的用户对象,而不是按请求或会话进行过滤,并使用
未经身份验证的答复。这似乎是一个过于基本的问题,因为放大函数将返回最后一个登录和验证的用户的用户对象,而不管来自其他地方的任何新传入请求的身份验证状态如何

两个身份验证检查:

function checkAuthenticated(req, res, next) {

    auth.currentAuthenticatedUser()
    .then(user => {
        console.log(user);
        next();
    })
    .catch(err => {
        console.log(err);
        return res.redirect('/login');
    });
}

function checkNotAuthenticated(req, res, next) {

    auth.currentAuthenticatedUser()
    .then(user => {
        console.log(user);
        return res.redirect('/');
    })
    .catch(err => {
        console.log(err);
        next();
    });
}
API调用的示例:

app.get('/', checkAuthenticated, (req, res) => {
    res.render('index.ejs');
});

app.post('/login', checkNotAuthenticated, async (req, res, next) => {
    // Some code to login
});
感谢您的指导