node.js 如何使用 Passport.js 访问 Cookie 集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12258795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:15:56  来源:igfitidea点击:

How to access Cookie set with Passport.js

node.jscookiesloginpassport.js

提问by Lukas Olsen

I'm using Passport.js to achieve login to my Node-App. But in my app, I need to get access to the user's ID and currently, I don't have an idea how to achieve this thing!

我正在使用 Passport.js 来登录我的 Node-App。但是在我的应用程序中,我需要访问用户的 ID,目前,我不知道如何实现这一目标!

How can I access the user-id or should I send it in a cookie myself?

我如何访问用户 ID 或者我应该自己在 cookie 中发送它?

回答by AnduA

You should introduce the following code in your app, next to the configuration of the strategies:

您应该在您的应用程序中,在策略配置旁边引入以下代码:

passport.serializeUser(function(user, done) {
   done(null, user.id);
});

passport.deserializeUser(function(obj, done) {
   done(null, obj);
});

In this way, when you invoke the donefunction with the authenticated user, passport takes care of storing the userId in a cookie. Whenever you want to access the userId you can find it in the request body. (in express req["user"]).

这样,当您done使用经过身份验证的用户调用该函数时,passport 负责将 userId 存储在 cookie 中。每当您想访问 userId 时,您都可以在请求正文中找到它。(在快递 req["user"])。

You can also develop the serializeUserfunction if you want to store other data in the session. I do it this way:

serializeUser如果您想在会话中存储其他数据,您也可以开发该功能。我这样做:

passport.serializeUser(function(user, done) {
   done(null, {
      id: user["id"],
      userName: user["userName"],
      email: user["email"]
   });
});

You can find more here: http://passportjs.org/docs/configure

您可以在此处找到更多信息:http: //passportjs.org/docs/configure

回答by user1071182

Add to signin path

添加到登录路径

res.cookie('userid', user.id, { maxAge: 2592000000 });  // Expires in one month

Add to signout path

添加到注销路径

res.clearCookie('userid');

回答by Tom S?derlund

If you're using the angular-fullstackgenerator, this is how I modified setUserCookieto get the _idin the user cookie (which I later can retrieve in AngularJS).

如果您使用的是angular-fullstack生成器,这就是我修改setUserCookie以获取_id用户 cookie(稍后我可以在 AngularJS 中检索)中的方式。

setUserCookie: function(req, res, next) {
    if (req.user) {
        req.user.userInfo['_id'] = req.user._id;
        console.log('Cookie', req.user.userInfo);
        // Splice in _id in cookie
        var userObjWithID = {
            "provider": req.user.userInfo.provider,
            "role": req.user.userInfo.role,
            "name": req.user.userInfo.name,
            "_id": req.user._id
        };
        res.cookie('user', JSON.stringify(userObjWithID));
    }
    next();
}

回答by Ally Haire

Alternatively you could do the following:

或者,您可以执行以下操作:

passport.serializeUser(User.serializeUser());

passport.deserializeUser(User.deserializeUser());
 app.use((req, res, next) => {
  res.locals.login = req.isAuthenticated();
  res.locals.thisUser = req.user;
  next();
});