护照Facebook总是重定向

时间:2018-05-31 18:48:03

标签: node.js facebook authentication callback

首先,我要感谢所有读过我帖子的人
这里的问题是:
我从jaredhanson使用护照facebook auth,但是一旦我最终设法登录,现在每次我去/ auth / facebook(甚至在我的智能手机上)它总是重定向到/ auth / facebook / callback?code = XYC ...而它应该每次都重定向到Facebook登录页面。
这是我的代码:

passport.use(new FacebookStrategy(
  {
    clientID: config.facebook.oauth.clientId,
    clientSecret: config.facebook.oauth.clientSecret,
    callbackURL: config.facebook.oauth.callbackURL,
    profileFields: config.facebook.oauth.profileFields,
  },
  ((accessToken, refreshToken, profile, done) => {
    User.findOrCreateFacebook(profile, (err, user) => {
      return done(err, user);
    });
  }),
));
router.get('', passport.authenticate('facebook'));
router.get(
  '/callback',
  passport.authenticate('facebook', {
    failureRedirect: '/facebook',
  }), (req, res) => {
    const profile = {
      id: req.user._id, //eslint-disable-line
      firstname: req.user.FName,
      lastname: req.user.LName,
      email: req.user.email,
      profilPic: req.user.photoProfil,
    };
    return res.status(200).json({
      success: true,
      token: signToken(profile),
    });
  },
);
UserSchema.statics.findOrCreateFacebook = function findOrCreateFacebook(profile, callback) {
  const user = new this();
  this.findOne({ facebookId: profile.id }, (err, result) => {
    // Be careful of the case duplicate mail
    if (!result) {
      user.email = profile._json.email;
      user.facebookId = profile._json.id;
      user.LName = profile._json.last_name;
      user.gender = profile._json.gender;
      user.FName = profile._json.first_name;
      user.photoProfil = profile.photos.length > 0 ? profile.photos[0].value : null;
      user.password = 'facebook'; // useless thing..
      user.save((error) => {
        callback(error, user);
      });
    } else {
      callback(err, result);
    }
  });
};
  passport.serializeUser((user, done) => {
    done(null, user);
  });
  passport.deserializeUser((user, done) => {
    done(null, user);
  });
  app.use(passport.initialize());
  app.use(passport.session());

老实说,我变得疯狂:(

0 个答案:

没有答案