gpt4 book ai didi

angularjs - 检查 401,然后重定向 Angularjs,但仅在某些路由上

转载 作者:行者123 更新时间:2023-12-03 07:01:03 24 4
gpt4 key购买 nike

我有一个很好的 angular.service ,可以通过查看服务器是否返回 401 错误消息来检查用户是否登录。如果是,用户将被重定向到登录页面。

这效果很好,但问题是它对于我的所有页面来说都是完全全局的。我有一个位于 /explore 的特殊路由(顺便说一句,使用 UI-router),我想忽略任何 401 并显示页面,而不重定向到登录页面。我该如何为 /explore 破例?我的代码目前如下:

  .factory('authHttpResponseInterceptor',['$q','$location',function($q,$location) {

return {
response: function(response){
if (response.status === 401) {
}

return response || $q.when(response);
},
responseError: function(rejection) {
if (rejection.status === 401) {
$location.path('/welcome');
}
return $q.reject(rejection);
}
};
}])

最佳答案

这应该可行,传入您希望用户重定向的特定路径:

var reservedPaths = ['/explore','/teachers','/reports'];

if (rejection.status === 401 && _.contains(reservedPaths,$location.path().trim()) {
$location.path('/welcome');
}

或者,如果您想使用ui-router的状态,您可以使用$state.includes()。如果上述方法不起作用,请提供一个小程序来演示问题。

关于 @Kanh TO 关于客户端能够查看未经授权的页面的评论,我们所做的是处理客户端的受限路由,但我们的 Web API 端点会检查身份验证/授权请求,因此即使用户确实摆弄并设法到达未经授权的路线,他们只会看到一个没有数据的 View ,在我看来这没什么大不了的,因为数据是您希望未经授权的用户看不到的内容,而不是您的应用程序的 View 。

关于angularjs - 检查 401,然后重定向 Angularjs,但仅在某些路由上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23178445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com