codecubed
codecubed

Reputation: 780

Why does Passport call failureRedirect only for authenticated new users?

Whenever a new user tries to log in, failureRedirect is called instead of sending them to the success link, even though the credentials are correct. What could I do to fix this?

Upvotes: 0

Views: 23

Answers (1)

codecubed
codecubed

Reputation: 780

failureRedirect is called because my user is returned as a falsey value. The source of this error was with how I handled MongoDB; Mongoose was returning the old doc (before upserting), which ended up being null. To fix this, if you are using Mongoose, you can add the option new: true during the .findOneAndUpdate(), and this will return the newly-created doc. Because this is a truthy value, Passport will detect this as an authenticated user.


User.findOneAndUpdate({ googleID: profile.id as string }, {$set: {
  googleID: profile.id,
  displayName: profile.displayName,
  emails: profile.emails as any,
  photos: profile.photos
}}, {upsert: true, >>> new: true <<<}, (err, result) => {
  done(err, result);
});

Upvotes: 1

Related Questions