【发布时间】:2017-01-11 19:09:39
【问题描述】:
我很抱歉这个问题有点长,但我已经研究了一段时间,真的需要解释所有的故事。
背景:基于 MEAN 堆栈的应用,尝试使用 Passport.js授权 Facebook 登录。
在 Passport.js guide 之后,我实现了类似的东西:
// HTML
<a href="/connect/facebook" target="_self">Add a Facebook login</a>
// send to facebook to do the authentication
app.get('/connect/facebook',isLoggedIn, passport.authorize('facebook',
{ scope : 'email' })
);
// handle the callback after facebook has authorized the user
app.get('/connect/facebook/callback',
passport.authorize('facebook', {
successRedirect : '/profile',
failureRedirect : '/profile'
}));
注意 html 中的 target=_self 以跳过 Angular 路由。
显然,授权工作正常。但是,重定向不起作用,因为路由是由 Angular 处理的。授权后,我从未登陆/profile(但在默认的Angular路线上)。
因此,我尝试使用 Passport.js here 建议的自定义回调,希望将 json 数据传递给 Angular,并让 Angular 进行路由。我最终做了类似的事情:
// In the controller
$http.get("/connect/facebook").success(function(data){
// here I wait for json data from the server and do the routing
});
// I call this route from Angular
app.get('/connect/facebook',isLoggedIn,passport.authorize('facebook',
{ scope : 'email' })
);
// But Facebook lands here!
app.get('/connect/facebook/callback',function(req, res, next) {
passport.authorize('facebook', function(err, user, info) {
res.json({something:smtg});
...
正如 Passport.js 所解释的,显然自定义回调适用于本地登录。但是在这里你看到问题了吗?我从 Angular 调用 /connect/facebook,但我应该从 /connect/facebook/callback 收到一些 json。
我即将放弃 Passport,但在此之前,您是否看到任何解决方案可以在 FB 授权后允许登陆 /profile,也许带有自定义消息?非常感谢您的阅读。
编辑: 同样的问题已在Passport-Facebook GitHub account 上报告为问题。那里已经发布了一些额外的尝试,但还没有完全修复。
【问题讨论】:
-
Angular 如何拦截重定向?您是否在您的 Express 应用程序中定义了
'/profile'路由?它有什么作用? -
我在 Angular 中有一个
/profile路由,在 Express 中没有/profile路由,在 Express 中有一个app.get('*',..)路由。它以角度提供默认路由:.otherwise({ redirectTo: '/'});。任何尝试使用 Express 中的res.render或res.redirect(to a specific angular view)都是徒劳的。
标签: angularjs node.js express passport.js passport-facebook