简体   繁体   中英

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.

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.

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).

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?

The info needs to stay after refreshes so $scope isn't an option from what I've read.

What you probably want to do here, is create a custom route for fetching this information. for example: "/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.

我希望您正在使用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. 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. What basically happens is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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