【问题标题】:Passport js - Add functionality to link accountsPassport js - 添加功能以链接帐户
【发布时间】:2020-04-20 22:54:24
【问题描述】:

我有一个用例,我需要 passport js 来检查用户是否已存在于 req.user 对象中。

如果用户存在 -> 不要创建新帐户,而是更新当前帐户

如果用户不存在 -> 创建一个新帐户

有什么方法可以检查req.user 是否在中未定义>

passport.use( new GoogleStrategy({

},(accessToken, refreshToken, profile, done) => {
   //Check if user already exists
   typeof(req.user)==='undefined' ? foo() : goo()
}));

堆栈:Passport.js、Express、Mongoose、Nodejs

【问题讨论】:

    标签: javascript node.js express passport.js


    【解决方案1】:

    回答

    答案很简单。只需在回调函数中添加req 参数和在策略选项 中添加passReqToCallback: true。我希望同样的问题不会浪费其他开发人员的时间。干杯?

    passport.use(new TwitterStrategy({
        consumerKey: TWITTER_CONSUMER_KEY,
        consumerSecret: TWITTER_CONSUMER_SECRET,
        callbackURL: "http://www.example.com/auth/twitter/callback",
        passReqToCallback: true //Don't forgot to add this option
      },
      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);
        }
      }
    ));
    

    【讨论】:

      猜你喜欢
      • 2023-02-15
      • 2011-08-26
      • 2017-12-19
      • 1970-01-01
      • 2018-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多