【问题标题】:CastError: Cast to ObjectId failed for value "XXXXXXXXXX" (type string) at path "_id" for model "User" for passport-linkedin-oauth2CastError:对于passport-linkedin-oauth2的模型“User”,路径“_id”的值“XXXXXXXXXX”(类型字符串)转换为ObjectId失败
【发布时间】:2022-01-21 07:31:25
【问题描述】:

尝试使用 LinkedIn 的 oAuth 2.0 对新用户进行身份验证时出现上述错误。看起来我正在传递一个 10 位的 ObjectId 而不是 12 或 24 个字符的 id...

passport.use(new LinkedInStrategy({
  clientID: process.env.LI_ID,
  clientSecret: process.env.LI_SECRET,
  callbackURL: "http://localhost:3000/auth/linkedin/think-it",
  scope: ['r_emailaddress', 'r_liteprofile'],
  state: true
}, function(accessToken, refreshToken, profile, done) {
  // asynchronous verification, for effect...
  process.nextTick(function () {
    // To keep the example simple, the user's LinkedIn profile is returned to
    // represent the logged-in user. In a typical application, you would want
    // to associate the LinkedIn account with a user record in your database,
    // and return that user instead.
    return done(null, profile);
  });
}));

我要挂断电话了:

passport.deserializeUser(function(id, done) {
  User.findById(id, function(err, user) {
    done(err, user);
  });
});

【问题讨论】:

    标签: javascript mongoose oauth-2.0 passport.js linkedin


    【解决方案1】:

    已解决。我没有从我的数据库返回用户。从上面的代码:

    ...function(accessToken, refreshToken, profile, done) {
      // asynchronous verification, for effect...
      process.nextTick(function () {
        //code below was not present when I was getting the error:
        User.findOrCreate({ linkedInId: profile.id}, function(err, user) {
              if (err) { return done(err); }
              done(null, user);
            });
      });
    

    【讨论】:

      猜你喜欢
      • 2021-09-06
      • 1970-01-01
      • 2023-02-16
      • 2020-12-10
      • 1970-01-01
      • 2022-09-29
      • 1970-01-01
      • 2021-12-04
      • 2020-11-19
      相关资源
      最近更新 更多