javascript - angularjs延迟加载应用程序直到指定条件

标签 javascript html angularjs user-experience

如果用户未经过身份验证,我的 Angular 应用程序 (SPA) 需要重定向到另一台服务器。这是一台单独的机器,这意味着从我的 Angular 应用程序重定向到此身份验证服务器时可能会出现延迟。

我希望实现的目标如下:

当应用程序被请求并加载时,它要么不显示内容,要么显示普通/简单页面。

如果应用程序发现用户未登录或登录已过期,那么它将继续显示该普通页面,同时将应用程序重定向到此身份验证服务器。

非常感谢您对此的投入。

谢谢

编辑: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 的建议类似。但问题是登陆页面被完全加载,然后它被重定向到身份验证服务器。我知道问题在于它们是单独的服务器,因此它们可以有不同的响应时间。

最佳答案

我使用一些东西在 mvc 5 应用程序中完成此任务。其中最重要的组件是AuthorizeInterceptor。我在我的咖啡/JavaScript 中使用了一个类,与您在大多数示例中看到的不同,但主要原理是相同的。我就不给你喝咖啡了,这里有一些 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 时,它将将此人重定向到应用程序的登录页面。

关于javascript - angularjs延迟加载应用程序直到指定条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26217431/

相关文章:

javascript - 对 prettyPhoto 灯箱进行动态 api 调用

javascript - 通过 JSON 字段搜索名称

php - 移除模块并连接一个新模块

javascript - 如何使 chrome 扩展与页面上的 Angular 对话

javascript - 使用 angular 1.3 ngMessage 处理表单提交错误

javascript - jquery中的扩展函数

html - 背景透明度 crossbrowser 特别是 ie

jquery - 创建一个 16 :9 aspect ratio iframe based on browser size (percentage)

javascript - 我如何做一个 sinon stub 来替换 promise ,以便响应被 mock ?

javascript - 从 react 应用程序连接到 mongodb 给出 TypeError : Cannot read property 'replace' of undefined