【问题标题】:Not able to reach successRedirect using passport for facebook无法使用 facebook 的护照成功重定向
【发布时间】:2016-04-16 08:32:33
【问题描述】:

我正在编写一个“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 中指定的路径?

【问题讨论】:

    标签: javascript html node.js passport-facebook


    【解决方案1】:

    使用此代码:

    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.
    

    【讨论】:

      【解决方案2】:

      如果您要更改端点,您还需要将 FacebookStrategy 中的 callbackURL 设置为 '/auth/facebook/callback' :)

      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('/');
      }
      

      【讨论】:

        猜你喜欢
        • 2018-03-19
        • 1970-01-01
        • 2018-11-19
        • 1970-01-01
        • 2017-10-13
        • 2019-03-08
        • 2018-03-16
        • 2019-01-06
        • 2019-05-23
        相关资源
        最近更新 更多