使用$ cookies和$ stateChangeStart检查sessionID是否正在执行最大callstack(Using $cookies and $stateChangeStart to c

编程入门 行业动态 更新时间:2024-10-11 03:24:54
使用$ cookies和$ stateChangeStart检查sessionID是否正在执行最大callstack(Using $cookies and $stateChangeStart to check for sessionID exeeding maximum callstack)

我不确定为什么会这样。 我正在尝试收听我的Angular应用程序中的每个状态更改,如果他们不是Auth,则将用户推回登录状态。 我将他们加密的sessionID存储在一个cookie中,检查该cookie是否未定义,并重定向用户......或者至少是我正在尝试做的事情。 我将使用toState和toParams参数进行用户角色等等,因此您可以暂时忽略这些参数作为此任务的一部分。

任何反馈都会有所帮助。 提前致谢!

.run(($rootScope, $state, $cookies) => { $rootScope.$on('$stateChangeStart', (evt, toState, toParams) => { if(!$cookies.get('SessionID')){ evt.preventDefault(); $state.go('login'); } }) })

I'm not really sure why this is happening. I'm trying to listen to each state change in my Angular application and push the user back to login if they're not Auth. I'm storing their encrypted sessionID in a cookie, checking if that cookie is undefined and the redirecting the user as such.. or at least that's what I'm trying to do. I will be using the toState and toParams arguments down the road for user role's etc. so you can ignore those as part of this task for now.

Any feedback would be helpful. Thanks in advance!

.run(($rootScope, $state, $cookies) => { $rootScope.$on('$stateChangeStart', (evt, toState, toParams) => { if(!$cookies.get('SessionID')){ evt.preventDefault(); $state.go('login'); } }) })

最满意答案

由于您在每次状态更改时都这样做,因此您创建了一个无限循环。 你可能会对此有所了解,但这应该会让你在那里:

.run(($rootScope, $state, $cookies) => { $rootScope.$on('$stateChangeStart', (evt, toState, toParams) => { if(!$cookies.get('SessionID')) { if (toState.name !== "login") { evt.preventDefault(); $state.go('login'); } } }) })

上面的内容将帮助您入门,但是当您继续前进时,您会发现还有其他状态您希望在没有Cookie或身份验证的情况下允许访问。 处理此问题的最佳方法可能是您在每个州上定义的自定义属性。 因此,当您定义状态时,您将执行以下操作:

.state('home', {url: '/home', templateUrl: 'views/home.html', authRequired: false}) .state('someSecureUrl', {url: '/someSecure.url', templateUrl: 'views/someSecureUrl.html', authRequired: true})

然后将上面的代码修改为:

.run(($rootScope, $state, $cookies) => { $rootScope.$on('$stateChangeStart', (evt, toState, toParams) => { if(!$cookies.get('SessionID') && toState.authRequired) { evt.preventDefault(); $state.go('login'); } }) })

您还可以使用州的解析功能来查看此操作。

Since you're doing this on every state change, you've created an infinite loop. you might pretty this up a bit, but this should get you there:

.run(($rootScope, $state, $cookies) => { $rootScope.$on('$stateChangeStart', (evt, toState, toParams) => { if(!$cookies.get('SessionID')) { if (toState.name !== "login") { evt.preventDefault(); $state.go('login'); } } }) })

The above will get you started, but as you move forward you'll find that there are other states that you want to allow access to without cookies or authentication as well. The best way to handle this is probably with a custom property that you define on each state. So when you define your states you would do something like:

.state('home', {url: '/home', templateUrl: 'views/home.html', authRequired: false}) .state('someSecureUrl', {url: '/someSecure.url', templateUrl: 'views/someSecureUrl.html', authRequired: true})

then modify the code above to be something like:

.run(($rootScope, $state, $cookies) => { $rootScope.$on('$stateChangeStart', (evt, toState, toParams) => { if(!$cookies.get('SessionID') && toState.authRequired) { evt.preventDefault(); $state.go('login'); } }) })

You can also look at doing this using a resolve function on your states.

更多推荐

本文发布于:2023-08-05 16:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1437202.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:sessionID   stateChangeStart   cookies   callstack   maximum

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!