<legend id='yUbQH'><style id='yUbQH'><dir id='yUbQH'><q id='yUbQH'></q></dir></style></legend>

    <i id='yUbQH'><tr id='yUbQH'><dt id='yUbQH'><q id='yUbQH'><span id='yUbQH'><b id='yUbQH'><form id='yUbQH'><ins id='yUbQH'></ins><ul id='yUbQH'></ul><sub id='yUbQH'></sub></form><legend id='yUbQH'></legend><bdo id='yUbQH'><pre id='yUbQH'><center id='yUbQH'></center></pre></bdo></b><th id='yUbQH'></th></span></q></dt></tr></i><div id='yUbQH'><tfoot id='yUbQH'></tfoot><dl id='yUbQH'><fieldset id='yUbQH'></fieldset></dl></div>
    <tfoot id='yUbQH'></tfoot>

        <bdo id='yUbQH'></bdo><ul id='yUbQH'></ul>
      1. <small id='yUbQH'></small><noframes id='yUbQH'>

        推迟 Angular UI 路由器 $stateChangeStart 直到收到服务器授权响应

        时间:2023-09-30
            <bdo id='KzTwQ'></bdo><ul id='KzTwQ'></ul>
                <i id='KzTwQ'><tr id='KzTwQ'><dt id='KzTwQ'><q id='KzTwQ'><span id='KzTwQ'><b id='KzTwQ'><form id='KzTwQ'><ins id='KzTwQ'></ins><ul id='KzTwQ'></ul><sub id='KzTwQ'></sub></form><legend id='KzTwQ'></legend><bdo id='KzTwQ'><pre id='KzTwQ'><center id='KzTwQ'></center></pre></bdo></b><th id='KzTwQ'></th></span></q></dt></tr></i><div id='KzTwQ'><tfoot id='KzTwQ'></tfoot><dl id='KzTwQ'><fieldset id='KzTwQ'></fieldset></dl></div>

                <small id='KzTwQ'></small><noframes id='KzTwQ'>

                  <legend id='KzTwQ'><style id='KzTwQ'><dir id='KzTwQ'><q id='KzTwQ'></q></dir></style></legend><tfoot id='KzTwQ'></tfoot>
                    <tbody id='KzTwQ'></tbody>

                • 本文介绍了推迟 Angular UI 路由器 $stateChangeStart 直到收到服务器授权响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I have an Angular app using UI Router where I'm trying to validate a user's token, if one exists, when the app runs. I am also checking that the user has permission to access certains routes. The problem is that $stateChangeStart is running before I receive the response back from the authorization endpoint. Here's some code (coffeescript with js below) - this is all within my run block.

                  app.run(($rootScope, $state, $stateParams, $log, Auth) ->
                  
                    currentState = 'home'
                  
                    $rootScope.$state = $state
                  
                    # read a cookie if cookie exists
                    if Auth.setAuthenticationToken()
                      # hit api endpoint to validate token
                      Auth.validateToken (user) ->
                        # route to current state
                        # this returns after $stateChangeStart runs below
                        $state.go(currentState)
                  
                    $rootScope.$on '$stateChangeStart', (event, toState, toParams, fromState, fromParams) ->
                  
                      currentState = toState.name
                  
                      Auth.setAuthenticationToken()
                  
                      $rootScope.error = null
                  
                      # compare users access permissions with incoming route's access level
                      if (!Auth.authorize toState.data.access, Auth.user)
                        event.preventDefault()
                        $rootScope.error = "Sorry, you haven't provided the required credentials."
                        $log.warn $rootScope.error
                  )
                  

                  My question is how can I get the $stateChangeStart to run only after the response from the auth endpoint has been returned. This only needs to happen the first time. Every subsequent state change can be done without hitting the auth endpoint.

                  解决方案

                  I'd create a function in your Auth service that returns a promise. Later, resolve (authorized) or reject (not authrized) that deferred. Then use it on the resolve property of your route definitions

                  $stateProvider.state('stateName',{
                      ...
                      ...
                      resolve: {
                           isAuthorized: function(Auth){
                               return Auth.checkAuthorization();
                           }
                      }
                  })
                  

                  To support subsequent checks you could maintain a promise within the service This might look like:

                  myApp.service('Auth',function($http,$q){
                      var authStatusDeferred = $q.defer();
                      this.checkAuthorization = function(){
                          return authStatusDeferred.promise;
                      };
                  
                      this.validateToken = function(user){
                          var isValid = false;
                          //..do validation stuff
                          if(isValid) authStatusDeferred.resolve();
                          //Otherwise state change will not happen..            
                      };
                  
                  
                  
                  });
                  

                  oh, sorry about no coffee

                  这篇关于推迟 Angular UI 路由器 $stateChangeStart 直到收到服务器授权响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Angular ui-router 中的 Promise 依赖解析顺序 下一篇:AngularJS - 具有 UI-Router 视图和 $ocLazyLoad 解析语法的动态 $StateProvi

                  相关文章

                  <tfoot id='9CuCv'></tfoot>
                  1. <legend id='9CuCv'><style id='9CuCv'><dir id='9CuCv'><q id='9CuCv'></q></dir></style></legend>

                  2. <small id='9CuCv'></small><noframes id='9CuCv'>

                    1. <i id='9CuCv'><tr id='9CuCv'><dt id='9CuCv'><q id='9CuCv'><span id='9CuCv'><b id='9CuCv'><form id='9CuCv'><ins id='9CuCv'></ins><ul id='9CuCv'></ul><sub id='9CuCv'></sub></form><legend id='9CuCv'></legend><bdo id='9CuCv'><pre id='9CuCv'><center id='9CuCv'></center></pre></bdo></b><th id='9CuCv'></th></span></q></dt></tr></i><div id='9CuCv'><tfoot id='9CuCv'></tfoot><dl id='9CuCv'><fieldset id='9CuCv'></fieldset></dl></div>
                        <bdo id='9CuCv'></bdo><ul id='9CuCv'></ul>