我不确定为什么会这样。 我正在尝试收听我的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.
更多推荐
发布评论