我的Web应用程序登录过程如下,
一个。用户点击登录按钮,链接将他们带到http://localhost:3030/login/facebook
湾。NodeJS 后端接收请求,并使用 passport-facebook 将浏览器定向到 Facebook 登录页面。
C。然后,Facebook 使用一些用户数据将用户引导回回调。(回调:http://localhost:3030/login/facebook/callback)
d。使用用户数据,帐户要么存在,要么将被创建,并且将创建属于该帐户的令牌(使用 JWT)
e. 该令牌将通过重定向到http://localhost:3030/#/got_token/the_actual_jwt_string发送给用户
F。AngularJS 应用程序将使用 ngRoute 进行路由,并将令牌保存在 localStorage 中。
NodeJS 中的令牌发行代码,
server.get('/auth/facebook', passport.authenticate('facebook', {session: false}));
server.get('/auth/facebook/callback', passport.authenticate('facebook', {session: false}), function(req, res) {
var token = jwt.sign(/* issue a token */);
// put the token into database
res.header('Location', 'http://localhost:3030/#/got_token/' + token);
res.send(302);
});
AngularJS 中的路由代码,
.when('/got_token/:token*', {
redirectTo: function(parameters, path, search) {
localStorage.setItem('ngStorage-token', JSON.stringify({
token: parameters.token
}));
return '/';
}
})
这很好用,直到我的视图层次结构看起来像这样,
<body ng-controller="NavigationViewController">
<div ng-view>
</div>
</body>
和控制器代码,
controllers.controller('NavigationViewController', ['$scope', '$route', 'token', function($scope, $route, token) {
var _token = token.retrieve();
$scope.token = _token;
$scope.authenticated = (_token != '');
if ($scope.authenticated) {
var payload_string = b64utos(_token.split('.')[1]);
var payload = KJUR.jws.JWS.readSafeJSONString(payload_string);
$scope.displayName = payload.facebookDisplayName;
} else {
$scope.displayName = 'no';
}
$scope.logout = function() {
token.clear();
};
}]);
完成的路由ngRoute
不会重新创建 my NavigationViewController
,而是将_token
变量设置为先前的状态。
我需要一种方法来NavigationViewController
知道令牌已更改,因为它不参与路由。