2017-08-26 22 views
0

我正在使用PassportJS,它是Facebook的策略來爲我的網站對用戶進行身份驗證。通過PassportJS從Facebook處理取消選擇的值

當用戶不在Facebook彈出窗口中取消選擇任何內容時,所有這一切都很好。

但是,說用戶取消選擇電子郵件,我不會收回,這顯然是在用戶的控制。

我的問題是 - 我如何根據我重試的數據將他們引導到「別的地方」?

passport.use(new FacebookStrategy({ 
clientID: config.facebook.appID, 
clientSecret: config.facebook.appSecret, 
callbackURL: config.facebook.callbackURL, 
profileFields: ['displayName', 'picture.type(large)', 'emails', 'birthday', 'location']}, 
function(accessToken, refreshToken, profile, done){ 

    var newUser = new userModel({ 
     fullname : profile.displayName, 
     profilePic : profile.photos[0].value || '', 
     email  : profile.emails[0].value || '', 
     birthday : profile._json.birthday || '', 
     location : profile._json.location.name || '', 
    }); 

    newUser.save(function(err){ 
     done(null, newUser); 
    }); 
); 
} 

我當然可以做某種預處理器,但我不知道如果數據無效,我需要做什麼。例如,說「電子郵件」是必須的。我將如何告訴用戶:「由於您沒有提供必填的電子郵件字段,請填寫」,然後我將用戶帶到配置文件設置頁面。在Facebook的應用程序

我的回調URL是:

http://localhost:3000/auth/facebook/callback 

而且它的下面路線的NodeJS:

router.get('/auth/facebook/callback', passport.authenticate('facebook', { 
    successRedirect:'/wall', 
    failureRedirect:'/login' 
})); 

任何投入會有所幫助。

在此先感謝。

回答

0

我想在這裏用戶不會取消選擇電子郵件字段。 Facebook會爲您提供您請求的所有用戶數據。但是,如果Facebook中提供的用戶電子郵件沒有通過驗證,那麼您不會在此處獲得該信息 email:profile.emails[0].value,即電子郵件字段將爲空。如果您認爲電子郵件對您是強制性的,那麼在每次成功登錄後,只需編寫代碼以檢查電子郵件字段是否爲空或空。如果它是空的,那麼你創建一個表單,你可以讓用戶輸入他們的電子郵件。 我用的MongoDB和護照的代碼會是這樣,

var User = require('../models/user'); 
    passport.use(new FacebookStrategy({ 
     clientID: configAuth.facebookAuth.clientID, 
     clientSecret: configAuth.facebookAuth.clientSecret, 
     callbackURL: configAuth.facebookAuth.callbackURL, 
     profileFields: ['id', 'email', 'first_name', 'last_name'], 
     }, 
     function(token, refreshToken, profile, done) { 
     process.nextTick(function() { 
      User.findOne({ 'facebook.id': profile.id }, function(err, user) { 
      if (err) 
       return done(err); 
      if (user) { 
       return done(null, user); 
      } else { 
       var newUser = new User(); 
       newUser.facebook.id = profile.id; 
       newUser.facebook.token = token; 
       newUser.facebook.name = profile.name.givenName + ' ' + profile.name.familyName; 
       if(profile.emails !=null) 
    { 
       newUser.facebook.email = (profile.emails[0].value || '').toLowerCase(); 
    } 
    else 
    { 
    //write your code here to use the form and request the email from the user and then add that email to your database. 
    } 
       newUser.save(function(err) { 
       if (err) 
        console.log(err); 
       return done(null, newUser); 
       }); 
      } 
      }); 
     }); 
     })); 

現在,你不需要改變路線的NodeJS成功的重定向和失敗重定向。

+0

正是。我的問題是 - 如何將用戶重定向到具有該表單的特定頁面?我只看到successRedirect和failureRedirect。謝謝 –

+0

我編輯了我的答案,檢查它。 – Gousia

+0

嗨,夥計。我想我可能不清楚。我上面看到的是如何檢查變量是否提供。我看不出重定向是如何工作的。我一直在整個網站進行數據驗證,但我不知道如何根據提供的數據將用戶路由到特定頁面。我希望我現在清楚。謝謝 –