简体   繁体   中英

PassportJS and ExpressJS conditional URL redirect

I am using ExpressJS and PassportJS's Facebook strategy to try and perform some conditional redirects on the authentication callback that should be called when certain properties are missing.

My User Schema:

var UserSchema = new Schema({
  id: ObjectId,
  uid: String,
  facebookToken: String,
  username: String,
  password: String,
  salt: String,
  firstName: String,
  lastName: String,
  email: String,
  birthday: Date,
  gender: String,
  location: String,
  phone: Number,
  verifiedPhone: Boolean,
  interests: {
    culture: Boolean,
    food: Boolean,
    business: Boolean,
    family: Boolean,
    learning: Boolean,
    sports: Boolean,
    movies: Boolean,
    music: Boolean,
    events: Boolean,
    nightlife: Boolean,
    health: Boolean,
    beauty: Boolean,
    fashion: Boolean,
    motoring: Boolean,
    electronics: Boolean,
    groceries: Boolean,
    travel: Boolean,
    decor: Boolean
  },
  weeklyNotifcation: Number,
  weeklyNotificationSent: Number,
  created: {type: Date, default: Date.now}
});

My Callback:

app.get('/auth/facebook/callback', passport.authenticate('facebook', { failureRedirect: '/login' }), function(req, res) {
  User.findOne({uid: req.session.uid}, function(err, user) {
    if(user.email || user.location || user.phone || user.location == undefined) {
      res.redirect('/register');
    } else {
      res.redirect('/');
    }
  });
});

The data is stored into mongo after /auth/facebook is called. I check the DB against using the session and check to see if it undefined as it will not return the fields since they weren't stored in the DB. The callback fails and returns no data received.

Any help will be appreciated.

if(user.email === undefined || user.location === undefined || user.phone === undefined || user.location === undefined) {

is probably what you meant. Also, you should probably check the err variable to see if there are errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM