<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 直到收到服务器授权响应

        Defer Angular UI Router $stateChangeStart until server authorization response receieved(推迟 Angular UI 路由器 $stateChangeStart 直到收到服务器授权响应)
            <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 直到收到服务器授权响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))
                  <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'>

                      <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>

                              <tbody id='9CuCv'></tbody>