javascript DEFAULT passport-google-oauth

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript DEFAULT passport-google-oauth相关的知识,希望对你有一定的参考价值。

passport.use(new GoogleStrategy({
      clientID: process.env.GOOGLE_CONSUMER_KEY,
      clientSecret: process.env.GOOGLE_CONSUMER_SECRET,
      callbackURL: `${process.env.GOOGLE_CALLBACK_URL}`
    },
    function(accessToken, refreshToken, profile, done) {
      User.findOne({ email: profile.emails[0].value },
        function(err, user) {
          // handle immediate errors
          if (err) return catchErrors(err);
          // If a user with an email === google email, check for google.id
          if (user) {
            // if user's google schema info is undefined, set dat shit
            if (user.google.id === undefined) {
              user.google.id = profile.id;
              user.google.token = accessToken;
              user.google.email = profile.emails[0].value;
              user.google.name = profile.displayName;
            }
            if (user.name === undefined) {
              user.name = profile.givenName + profile.familyName;
            }
            user.save();
            return done(null, user);
          } else { // If no user exists with email === google email, create new User
            const user = new User();
            // local data
            user.name = profile.displayName;
            user.email = profile.emails[0].value;
            user.google.id = profile.id;
            user.google.token = accessToken;
            user.google.name = profile.displayName;
            user.google.email = profile.emails[0].value;
            user.save(function(err) {
              if (err) console.log(err);
              return done(err, user);
            });
          }
        }
      );
    }
));

以上是关于javascript DEFAULT passport-google-oauth的主要内容,如果未能解决你的问题,请参考以下文章

javascript default.js

javascript 用于社交身份验证的DEFAULT用户架构

Typescript 编译的 Javascript 代码不起作用(variable.default.function())

javascript 社交提供商的DEFAULT护照身份验证

javascript DEFAULT passport-google-oauth

JavaScript函数的默认参数(default parameter)