护照Facebook身份验证与AngularJS路由提供者

3
我是一名有用的助手,可以为您翻译文本。

我有一个使用AngularJS和Node.js Express框架开发的应用程序。当我尝试实现Facebook登录(passport)时遇到了问题。

app.get('/auth/facebook',passport.authenticate('facebook'));
    app.get('/auth/facebook/callback',passport.authenticate('facebook',
        {
            successRedirect: '/profile',
            failureRedirect: '/'
        }));

以下是配置信息 -
passport.use(new FacebookStrategy({
            clientID: 'xxxx',
            clientSecret: 'xxxx',
            callbackURL:'http://campustop.in/auth/facebook/callback'
        },
        function(accessToken,refreshToken,profile,done){
            console.log(profile);
        }
    ));

因此,问题在于如果我在客户端尝试使用-
<a href="/auth/facebook">Login with Facebook</a>

然后,angularjs的$routeProvider首先捕获这个路由,并显示一个空白布局,因为没有在此URL中指定模板或控制器。所以,这个链接从未到达服务器中我的express路由配置的路由函数。 但是,当我直接在浏览器中打开此URL时,它会到达服务器,但随后没有被重定向到任何页面。它只停留在那里,并在网络选项卡中显示以下响应。
Request URL:https://www.facebook.com/dialog/oauth?response_type=code&redirect_uri=http%3A%2F%2Fcampustop.in%2Fauth%2Ffacebook%2Fcallback&client_id=1440645942863962
Status Code:302 forced.302

在网络选项卡中,您可以查看http://campustop.in/auth/facebook上的一些响应标头。请检查它们。
1个回答

9

我通过在链接标签中添加target="_self"解决了这个问题。

<a href="/auth/google" target="_self">Sign in with Google</a>

当您这样做时,Angular路由提供程序会忽略它并允许请求命中您的服务器。

你是一个伟大的英雄 - AHSAN KHAN

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接