【问题标题】:ExpressJS and Angular - 'leaving' the app to do server-side authenticationExpressJS 和 Angular - “离开”应用程序进行服务器端身份验证
【发布时间】:2013-07-22 07:07:08
【问题描述】:

我的应用在 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'
      }).

但这会加载一个空白屏幕。如何让两者协同工作?

【问题讨论】:

标签: javascript angularjs node.js express passport.js


【解决方案1】:

添加 target="_self" 在 Angular 1.0.1 中有效:

使用 Facebook 登录

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

如果您好奇,请查看角度源代码(第 5365 行 @ v1.0.1)。只有当 !elm.attr('target') 为 true 时才会发生点击劫持。

【讨论】:

  • 一个问题 - 我从应用程序内部开始,然后进行身份验证,然后离开应用程序。我如何回到应用程序的“内部”?如上所述,我目前被带到/fbauthed,但这显然不在任何控制器范围内......
  • 当 /fbauthed 指向控制器时,您应该为角度设置路由
  • 搞定了,必须将我的 node/express 应用程序中的上述代码更改为 app.get('/fbauthed', passport.authenticate('facebook',{ failureRedirect: '/' }), <INSERT ANGULAR ROUTE HERE>);
猜你喜欢
  • 2017-07-08
  • 2018-01-12
  • 2014-02-04
  • 1970-01-01
  • 1970-01-01
  • 2015-07-03
  • 1970-01-01
  • 2020-06-16
  • 1970-01-01
相关资源
最近更新 更多