2013-01-01 56 views

回答

3

我使用此解決方案,這是很容易的。我保存會話中的最後一個網址,然後收集。如果您直接將回叫網址傳遞給fb,您需要允許所有不同的路線,這是非常令人頭疼的。

app.get '/auth/facebook/', routesService.clean, (req, res, next) -> 
    req.session.redirect = req.query.redirect if req.session? && req.query.redirect? 
    passport.authenticate('facebook', { scope: ['user_friends'] }) req, res, next 
    return 

    app.get '/auth/facebook/callback', passport.authenticate('facebook', failureRedirect: '/login'), (req, res) -> 
     # Successful authentication, redirect home. 
     if req.session?.redirect? 
      rediredUrl = req.session.redirect 
      delete req.session.redirect 
      res.redirect '/#!'+ rediredUrl 
     else 
      res.redirect '/' 
     return 
1

剛上加的回答跟進,下面是組合解決方案:

app.get('/auth/facebook', (req, res, next) => { 
    passport.authenticate('facebook')(req, res, next) 
}) 

app.get("/auth/facebook/callback", (req, res, next) => { 
    passport.authenticate('facebook', (err, user, info) => { 
    if (err) { return next(err); } 
    if (!user) { return res.redirect('/login')} 
    req.logIn(user, function(err) { 
     if (err) { return next(err); } 
     return res.redirect('/users/' + user.uid) 
    }); 
    })(req, res, next) 
}) 

請注意,您必須專門登錄的用戶