Google OAuth抛出非法参数:未定义,字符串错误

时间:2020-03-10 14:05:15

标签: javascript node.js oauth-2.0 google-oauth passport-google-oauth

我正在尝试使用Passport.js在Express应用程序中实现OAuth,当我尝试使用自己的Google帐户登录并点击回调函数时,出现illegal arguments: undefined, string at Strategy.module.exports [as _verify]错误。当我从回调函数的参数列表中删除req参数时,配置文件变为未定义,但是当我拥有req并将配置文件登录到控制台时,我获取了数据,然后抛出了illegal arguments: undefined, string错误。我还要确保我在Google选项中通过了passReqToCallback:true。任何帮助将不胜感激,我已经坚持了一段时间。

passport.serializeUser((user, cb) => {
  return cb(null, user);
});
passport.deserializeUser((user, cb) => {
  return cb(null, user);
});
//googleOptions.js
 {
  clientID: process.env.googleClientID,
  clientSecret: process.env.googleClientSecret,
  callbackURL: 'http://localhost:5000/auth/google/callback',
  passReqToCallback: true
}
//googleCallback function
 async (req, accessToken, refreshToken, profile, done) => {
  try {
   // will log the profile so far as the parameter list is as is
   // will return undefined if req is missing from the parameter list
    console.log(profile);
    const user = await User.findOne({ googleId: profile.id });
    if (user) {
      done(null, user);
      return;
    }
    const salt = await bcrypt.genSalt(12);
    const hashedPassword = await bcrypt.hash(profile.token, salt);
    const newUser = new User({
      email: profile.emails[0].value,
      firstName: profile.name.displayName.split(' ')[0].join(''),
      lastName: profile.name.familyName,
      googleId: profile.id,
      password: hashedPassword
    });
    const token = newUser.generateAuthToken();
    req.token = token;
    await newUser.save();
    return done(null, newUser);
  } catch (error) {
    console.log(`Error Message: ${error}`);
    throw new Error(`Error Message: ${error.message}`);
  }
};

0 个答案:

没有答案