2014-10-06 34 views
0

如果用戶未通過身份驗證,我的角應用程序(SPA)需要重定向到另一臺服務器。這是一個單獨的機器,這意味着從我的角度應用程序重定向到此auth服務器時可能會有延遲。angularjs延遲加載應用程序直到指定條件

我尋找實現如下:

當應用程序請求和被加載,它要麼不顯示內容或顯示香草/簡單的頁面。

如果應用程序發現用戶未登錄或登錄過期,那麼它將繼續顯示香草頁面,同時將應用程序重定向到此auth服務器。

真的很感激在這方面的投入。

感謝,

編輯:interceptor.js代碼如下:

app.factory('authInterceptorService', ['$q', '$injector', '$location', 'localStorageService', 
     function ($q, $injector, $location, localStorageService) { 
.... 
     var request = function (config) { 
      config.headers = config.headers || {}; 
      var fragments = getFragment(); 
      if(fragments.access_token != undefined) 
       localStorageService.add("authorizationData", { token: fragments.access_token, token_type: fragments.token_type, state : fragments.state, }); 
      var authData = localStorageService.get('authorizationData'); 
      if(authData) 
      { 
       config.headers.Authorization = authData.token_type + ' ' + authData.token; 
       $location.path("/dashboard"); 
      } 
      else 
       logout(); 
      return config; 
     }; 

     var responseError = function (rejection) { 
      if (rejection.status === 401) { 
       logout(); 
      } 
      return $q.reject(rejection); 
     }; 

     var logout = function() 
     { 
      localStorageService.remove('authorizationData'); 
      var scope = 'my-scope'; 
        var uri = addQueryString('http://www.authserver.com/OAuth/Authorize', { 
         'client_id': 'dsfdsafdsafdsfdsfdsafasdf', 
         'redirect_uri': 'www.returnuri.com', 
         'state': 'asdfasfsdf', 
         'scope': 'scope1', 
         'response_type': 'token' 
         }); 
       window.location.replace(uri); 

     }; 

     authInterceptorServiceFactory.request = request; 
     authInterceptorServiceFactory.responseError = responseError; 
     authInterceptorServiceFactory.logout = logout; 

     return authInterceptorServiceFactory; 
    }]); 
}); 

這類似於正在被Blackunknown建議什麼。但問題是登陸頁面被完全加載,然後它被重定向到auth服務器。我知道問題在於它們是獨立的服務器,因此它們可能有不同的響應時間。

回答

0

由於您提供絕對沒有代碼,這裏是一個僞例如:

$http.get('yourAuthServer').success(function(response){ 
    // save session data and redirect the user to the regular page 
    $location.path('loggedInRoute'); 
}).error(function(err){ 
    // handle the failed authentification here 
    $location.path('authFailed'); 
}); 

所以,這個想法是有一個登陸頁面,沒有敏感數據。你將從主控制器發出一個認證請求,根據結果,你將正確地重定向用戶。當然,您應該在登錄頁面上進行身份驗證檢查,而不僅僅依賴於重定向。但那會讓你開始。

1

我使用了幾件事情來完成這個在mvc 5應用程序中完成。其中最重要的組件是AuthorizeInterceptor。我使用咖啡/ javascripts中設置的類,而不是在大多數示例中都會看到的,但主要原理是相同的。我會在這裏免去你的咖啡是一些javascript:

(function() { 
    "use strict"; 

    var AuthorizeConfig, AuthorizeInterceptor; 

    AuthorizeInterceptor = (function() { 
    function AuthorizeInterceptor($q, $location) { 
     this.$q = $q; 
     this.$location = $location; 
     return { 
     response: function(response) { 
      return response || $q.when(response); 
     }, 
     responseError: function(rejection) { 
      if (((rejection != null ? rejection.status : void 0) != null) && rejection.status === 401) { 
      $location.path("/Login"); 
      } 
      return $q.reject(rejection); 
     } 
     }; 
    } 

    return AuthorizeInterceptor; 

    })(); 

    angular.module("myapp").factory("AuthorizeInterceptor", ["$q", "$location", AuthorizeInterceptor]); 

    AuthorizeConfig = (function() { 
    function AuthorizeConfig($httpProvider) { 
     $httpProvider.interceptors.push("AuthorizeInterceptor"); 
    } 

    return AuthorizeConfig; 

    })(); 

    angular.module("myapp").config(["$httpProvider", AuthorizeConfig]); 

}).call(this); 

當在401請求的結果,將這個人重定向到應用程序的登錄頁面。