简体   繁体   English

NodeJS验证后如何使用AngularJS访问用户信息?

[英]How can I access User Info with AngularJS after NodeJS verification?

I've seen a few questions regarding this but being new to both Angular and Node I'm struggling to find a proper solution. 我已经看到了一些与此相关的问题,但是对于Angular和Node来说都是新手,所以我一直在努力寻找合适的解决方案。

I had this code, which worked for the login and I could only access my pages after being authenticated: 我有以下代码,该代码可用于登录,并且只有通过身份验证后才能访问我的页面:

router.post('/login',
    passport.authenticate('local',
            {
                successRedirect: '/',
                failureRedirect: '/login',
                failureFlash: false,
                successFlash: false
            }
    ));

router.all('*', ensureAuthenticated);

function ensureAuthenticated(req, res, next) {
    console.log("in ensureAuth", req.isAuthenticated());
    console.log("session data", req.session);
    console.log("user data", req.user);
    if (req.isAuthenticated())
    {
        return next();
    }
    res.redirect('/login');
}

The local passport authentication I have returns an object after verifying if the user exists and allows login, as such: 我进行的本地护照认证在验证用户是否存在并允许登录后返回一个对象,例如:

return passportDone(null, result.rows[0]);

with result.rows[0] being the user info I want. 与result.rows [0]是我想要的用户信息。

What I had in the front end is a simple ng-submit in the form that calls the "login(credentials)" with credentials being set by ng-model in their respective fields (username and password). 我在前端使用的是一个简单的ng-submit,其形式为调用“ login(credentials)”,其凭据由ng-model在其相应字段(用户名和密码)中设置。

My question is how can I return all the info I want, such as user_role, name and stuff, so I can present it in the front-end as {{user.name}} for example? 我的问题是如何返回我想要的所有信息,例如user_role,名称和材料,因此我可以将其以{{user.name}}的形式显示在前端?

The info needs to stay after refreshes so $scope isn't an option from what I've read. 刷新后需要保留信息,因此$ scope不是我所阅读的选项。

What you probably want to do here, is create a custom route for fetching this information. 您可能想在这里做的是创建一个用于获取此信息的自定义路由。 for example: "/me". 例如:“ / me”。

In this route, you can serve the information you now probably store in your session variable. 通过这种途径,您可以提供现在可能存储在会话变量中的信息。

Another solution, depending on how your application works with authentication, is to inject the userinfo (For example if you log in, and get redirected to a new page, you can inject the userinfo into the new page as a js variable) or to return the userinfo in the response if you send a ajax request to login and dont get redirected to a new page by the server. 取决于您的应用程序如何使用身份验证的另一种解决方案是注入userinfo(例如,如果您登录并重定向到新页面,则可以将js作为js变量将userinfo注入新页面)或返回如果您发送ajax登录请求,并且服务器未将其重定向到新页面,则响应中的userinfo。

我希望您正在使用passport.js本地身份验证。我建议将身份验证后的用户信息存储在cookie中作为json网络令牌或使用其他某种加密方式,并应公开一个api(/ api / is-authenticated)通过发送存储在cookie中的令牌来检查用户是否已通过身份验证。每当您刷新或导航到其他路由时,请进行api(/ api / is-authenticated)调用以检查该特定用户是否已通过身份验证。

Ok, I got it. 好,我知道了。

I already had the service and all but the problem was with passport's successfulRedirect. 我已经有了服务,但所有问题都出在护照的成功重定向上。 It returns the html you want when it succeeds, which is fine. 成功时返回所需的html,这很好。 However, since I wanted the user info, it wasn't enough. 但是,由于我需要用户信息,所以这还不够。 What I did was create an /account route that returns the req.user info, which I then handle in the front end and form a cookie with it. 我所做的是创建一个/ account路由,该路由返回req.user信息,然后我在前端进行处理并与之形成cookie。 What basically happens is: 基本上发生的是:

post_login->verify_user->success->get_account->form_cookie->render_html post_login-> verify_user->成功-> get_account-> form_cookie-> render_html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM