ExpressJS和Angular-'留下'应用程序进行服务器端身份验证

ExpressJS and Angular - 'leaving' the app to do server-side authentication

本文关键字:服务器端 身份验证 应用程序 ExpressJS 留下 Angular-      更新时间:2023-09-26

我的应用程序运行在NodeJS、ExpressJS和AngularJS上。我正在尝试使用PassportJS进行Facebook身份验证。为了避免发布一堆不必要的代码,让我们只说Passport代码按预期工作,并登录到Facebook,获取令牌并收到响应。

在ExpressJS中,我的路线设置如下:

app.get('/', routes.index);
app.get('/fbauth', passport.authenticate('facebook', { scope: 'email' }));
app.get('/fbauthed', passport.authenticate('facebook',{ failureRedirect: '/' }), routes.loggedin);
app.get('/logout', function(req, res) {
  req.logOut();
  res.redirect('/');
});

我的服务器设置在localhost:3000

问题

我的问题是Angular根本没有这样做。当我在我的页面上放一个指向href="/fbauth"的链接时(这会指向localhost:3000/fbauth),我看到地址栏在一瞬间变为localhost:3000/fbauth,然后我的主localhost:3000页面会重新加载。

但是,如果我在地址栏中键入localhost:3000/fbauth,身份验证就会发生,并且我会被传递到facebook->localhost:3000/fbauthed

我认为发生这种情况是因为Angular路由试图"接管"<a>链接中的URL,但我不知道如何实现。我已经尝试将我的Angular路由配置为:

when('/fbauth', {
        redirectTo: '/fbauth'
      }).

但这加载了一个空白屏幕。我怎样才能使两者协同工作?

添加target="_self"适用于Angular 1.0.1:

使用Facebook 登录

此功能已记录在案(http://docs.angularjs.org/guide/dev_guide.services.$location-搜索"_self")

如果你很好奇,可以看看角度源(第5365@v1.0.1行)。点击劫持只有在以下情况下才会发生!elm.attr("target")是真的。