无法使用Visa护照到达successRedirect

时间:2016-01-11 21:37:47

标签: javascript html node.js passport-facebook

我正在写一个" todolist"网络应用程序并使用 passport-facebook 进行第三方身份验证。这是我使用的代码:

passport.use(new FacebookStrategy({
   clientID: '566950043453498',
   clientSecret: '555022a61da40afc8ead59c6c26306ed',
   callbackURL: 'http://www.localhost:3000/auth/facebook/Todolistpage.html'
},
 function(accessToken, refreshToken, profile, done) {
    console.log("hello " + profile.displayName);

    done(null); 
 }
));

//Authentication
app.get('/auth/facebook', passport.authenticate('facebook'));

app.get('/auth/facebook/Todolistpage.html',
passport.authenticate('facebook', { successRedirect:'/auth/facebook/Todolistpage.html',
                                  failureRedirect: '/login' })); 

用户点击以下内容:

<a href="/auth/facebook">Login with Facebook</a>

我成功登录facebook,但我被重定向到 failureRedirect 部分代码中指定的路径。为什么这样做呢?如果成功登录facebook,如何让它进入 successRedirect 中指定的路径?

2 个答案:

答案 0 :(得分:2)

使用此代码:

app.get('/auth/facebook', passport.authenticate('facebook', { scope: [ 'email', 'user_about_me'], failureRedirect: '/', successRedirect: 'back' }));
app.get('/auth/facebook/callback', passport.authenticate('facebook', {failureRedirect: '/facebook' }), users.authCallback);

在服务器站点代码中:

exports.authCallback = function (req, res) {
    res.redirect('/');
} // this will help you to redirect specific page.

答案 1 :(得分:1)

你还需要在FacebookStrategy中将callbackURL设置为&#39; / auth / facebook / callback&#39;如果要更改端点:)

passport.use(new FacebookStrategy({
   clientID: '566950043453498',
   clientSecret: '555022a61da40afc8ead59c6c26306ed',
   callbackURL: 'http://www.localhost:3000/auth/facebook/callback'
}, function(accessToken, refreshToken, profile, done) {
    console.log("hello " + profile.displayName);
    done(null); 
 }
));

//Authentication
app.get('/auth/facebook', passport.authenticate('facebook'));

router.get('/auth/facebook/callback', passport.authenticate('facebook', {
    failureRedirect: '/login?failedSocial=facebook'
  }), auth.authCallback);

并在身份验证服务中

exports.authCallback = function (req, res) {
    res.redirect('/');
}