Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.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 Node.js Passport GoogleStrategy如何访问会话数据_Javascript_Node.js_Oauth 2.0_Passport.js - Fatal编程技术网

Javascript Node.js Passport GoogleStrategy如何访问会话数据

Javascript Node.js Passport GoogleStrategy如何访问会话数据,javascript,node.js,oauth-2.0,passport.js,Javascript,Node.js,Oauth 2.0,Passport.js,我使用在我的演示站点上实现注册/登录。它工作得很好。最近几天,我试图找到如何在“GoogleStrategy”实现中访问会话变量的解决方案 var GoogleStrategy = require('passport-google-oauth').OAuthStrategy; passport.use(new GoogleStrategy({ consumerKey: GOOGLE_CONSUMER_KEY, consumerSecret: GOOGLE_CONSUMER_SE

我使用在我的演示站点上实现注册/登录。它工作得很好。最近几天,我试图找到如何在“GoogleStrategy”实现中访问会话变量的解决方案

var GoogleStrategy = require('passport-google-oauth').OAuthStrategy;

passport.use(new GoogleStrategy({
    consumerKey: GOOGLE_CONSUMER_KEY,
    consumerSecret: GOOGLE_CONSUMER_SECRET,
    callbackURL: "http://127.0.0.1:3000/auth/google/callback"
},
function(token, tokenSecret, profile, done) {
    // Is it possible to access to session variables here, eg.:
    // var tmp = req.session.tmp;
    User.findOrCreate({ googleId: profile.id }, function (err, user) {
        return done(err, user);
    });
}));

我不知道这是否可能。我需要更新数据库中的现有用户,而不是创建新用户,但无法“获取”此函数中现有用户的id。

尝试添加passReqToCallback:true,它应该将req对象传递到回调中,然后将req对象作为回调中的第一个参数添加 例如 passport.use(谷歌新战略)({ passReqToCallback:正确, 消费者:… },函数(请求,令牌…

来自文档:

验证回调中的关联

将策略的passReqToCallback选项设置为true。启用此选项后,req将作为第一个参数传递给verify回调

passport.use(new TwitterStrategy({
    consumerKey: TWITTER_CONSUMER_KEY,
    consumerSecret: TWITTER_CONSUMER_SECRET,
    callbackURL: "http://www.example.com/auth/twitter/callback",
    passReqToCallback: true
  },
  function(req, token, tokenSecret, profile, done) {
    if (!req.user) {
      // Not logged-in. Authenticate based on Twitter account.
    } else {
      // Logged in. Associate Twitter account with user.  Preserve the login
      // state by supplying the existing user after association.
      // return done(null, req.user);
    }
  }
));

通过将req作为参数传递,verify回调可以使用请求的状态来定制身份验证过程,使用单个策略实例和一组路由来处理身份验证和授权。例如,如果用户已登录,则新“连接”的可以关联帐户。在req上设置的任何其他特定于应用程序的属性(包括req.session)也可以使用。

findOrCreate是否没有返回正确的用户?您的问题有点不清楚。findOrCreate只是一个示例,它可以正常工作……在调用findOrCreate方法之前,我需要从会话中获取变量。