在使用环回渲染 View 之前,如何确保用户已登录? 我可以使用我的角度应用程序登录前端。但我想阻止匿名用户查看该页面。
我以为它是一个 header ,比如 headers.authorization_token,但它似乎不存在。
我正在寻找类似于 connect-ensurelogin 的护照,而不必使用护照。
最佳答案
这就是解决您问题的 $interceptor。
此代码检测来自 Loopback REST 服务器的 401 响应(用户未登录或访问 token 已过期),并将用户重定向到登录页面:
// Inside app config block
$httpProvider.interceptors.push(function($q, $location) {
return {
responseError: function(rejection) {
if (rejection.status == 401) {
$location.nextAfterLogin = $location.path();
$location.path('/login');
}
return $q.reject(rejection);
}
};
});
一旦用户登录,此代码将重定向到请求的页面
// In the Login controller
User.login($scope.credentials, function() {
var next = $location.nextAfterLogin || '/';
$location.nextAfterLogin = null;
$location.path(next);
});
关于express - 环回保护路由/确保登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25876559/