Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 重置角度服务_Angularjs_Angularjs Service - Fatal编程技术网

Angularjs 重置角度服务

Angularjs 重置角度服务,angularjs,angularjs-service,Angularjs,Angularjs Service,有没有一种好方法可以在不创建依赖关系的情况下以角度重置工厂/服务中的数据 我目前有一个AuthService,它接受用户名和密码,并从服务器获取oauth令牌。我还有一个http拦截器,它将令牌添加到所有请求中 如果我得到401(未经授权)响应,则我的令牌不再有效,我希望将AuthService内的_AuthData设置为null。但我现在没有什么好办法 如果我在拦截器中添加了一个AuthService依赖项(以便能够调用LogOut()),那么我会得到一个循环引用,因为AuthService使


有没有一种好方法可以在不创建依赖关系的情况下以角度重置工厂/服务中的数据

我目前有一个AuthService,它接受用户名和密码,并从服务器获取oauth令牌。我还有一个http拦截器,它将令牌添加到所有请求中

如果我得到401(未经授权)响应,则我的令牌不再有效,我希望将AuthService内的_AuthData设置为null。但我现在没有什么好办法

如果我在拦截器中添加了一个AuthService依赖项(以便能够调用LogOut()),那么我会得到一个循环引用,因为AuthService使用$http

我一直在从AuthService内部的localstorageservice中重新读取IsAuthenticated()和Username()等方法的令牌,但我希望尽可能避免这种情况,以避免性能受到影响

有没有一种方法可以从AuthInterceptorService“重置”AuthService而不创建依赖性

AuthService

appRoot.factory("AuthService", ["$http", "$q", "localStorageService", function ($http, $q, localStorageService) {
    var _AuthData;

    var AuthServiceFactory = {};

    AuthServiceFactory.Username = function () {
        return _AuthData.Username;
    };

    AuthServiceFactory.Roles = function () {
        return _AuthData.Roles;
    };

    AuthServiceFactory.IsAuthenticated = function () {
        return _AuthData != null;
    };

    AuthServiceFactory.LogOut = function () {
        _AuthData = null;
        localStorageService.remove("AuthData");
    };

    AuthServiceFactory.Login = function (Username, Password) {
        var Deferred = $q.defer();
        $http.post(ApiBaseUrl + "token", Username, { headers: { 'Content-Type': "application/x-www-form-urlencoded" } }).success(function (Response) {
            _AuthData = {
                Token: Response.access_token,
                Username: Username,
                Roles: Response.Roles
            };
            localStorageService.set("AuthData", _AuthData);

            Deferred.resolve(Response);
        }).error(function (err, status) {
            Deferred.reject(err);
        });

        return Deferred.promise;
    };

    return AuthServiceFactory;
}]);
appRoot.factory("AuthInterceptorService", ["$q", "$location", "localStorageService", function ($q, $location, localStorageService) {
    var AuthInterceptorServiceFactory = {};
    AuthInterceptorServiceFactory.request = function (config) {
        config.headers = config.headers || {};

        var AuthData = localStorageService.get("AuthData");

        if (AuthData) {
            config.headers.Authorization = "Bearer " + AuthData.Token;
        }

        return config;
    };

    AuthInterceptorServiceFactory.responseError = function (Rejection) {
        if (Rejection.status === 401) {
            localStorageService.remove("AuthData");
            //AuthService.LogOut(); //Need to reset token here

            $location.url("/Login");
        }
        return $q.reject(Rejection);
    };

    return AuthInterceptorServiceFactory;
}]);
AuthInterceptorService

appRoot.factory("AuthService", ["$http", "$q", "localStorageService", function ($http, $q, localStorageService) {
    var _AuthData;

    var AuthServiceFactory = {};

    AuthServiceFactory.Username = function () {
        return _AuthData.Username;
    };

    AuthServiceFactory.Roles = function () {
        return _AuthData.Roles;
    };

    AuthServiceFactory.IsAuthenticated = function () {
        return _AuthData != null;
    };

    AuthServiceFactory.LogOut = function () {
        _AuthData = null;
        localStorageService.remove("AuthData");
    };

    AuthServiceFactory.Login = function (Username, Password) {
        var Deferred = $q.defer();
        $http.post(ApiBaseUrl + "token", Username, { headers: { 'Content-Type': "application/x-www-form-urlencoded" } }).success(function (Response) {
            _AuthData = {
                Token: Response.access_token,
                Username: Username,
                Roles: Response.Roles
            };
            localStorageService.set("AuthData", _AuthData);

            Deferred.resolve(Response);
        }).error(function (err, status) {
            Deferred.reject(err);
        });

        return Deferred.promise;
    };

    return AuthServiceFactory;
}]);
appRoot.factory("AuthInterceptorService", ["$q", "$location", "localStorageService", function ($q, $location, localStorageService) {
    var AuthInterceptorServiceFactory = {};
    AuthInterceptorServiceFactory.request = function (config) {
        config.headers = config.headers || {};

        var AuthData = localStorageService.get("AuthData");

        if (AuthData) {
            config.headers.Authorization = "Bearer " + AuthData.Token;
        }

        return config;
    };

    AuthInterceptorServiceFactory.responseError = function (Rejection) {
        if (Rejection.status === 401) {
            localStorageService.remove("AuthData");
            //AuthService.LogOut(); //Need to reset token here

            $location.url("/Login");
        }
        return $q.reject(Rejection);
    };

    return AuthInterceptorServiceFactory;
}]);

我可以想出一些非常合理的选择

>P>首先要考虑的是,本地存储的性能命中对你来说真的是个问题吗?您当前的解决方案简单易懂,这本身就是一个特性

  • AuthService
    拆分为
    授权人
    AuthStorage
    。这样,
    Authorizer
    就可以依赖于
    $http
    AuthStorage
    就不需要了,
    AuthInterceptorService
    就可以依赖于
    AuthStorage
    ,您可以在这里设置重置功能

  • 这一个感觉像一个大锤子,但是
    AuthInterceptorService
    可以在
    Approt
    上广播一个
    auth\u failed
    事件,而
    AuthService
    可以监听该事件以执行重置。这正朝着非常全局的消息传递方向发展,所以我会担心它的可维护性


  • 谢谢你的建议,克里斯特扬。我决定使用选项2,因为这似乎是最干净、最可维护的解决方案。回答#1,即使性能影响不大,直接从本地存储读取/写入拦截器似乎有点不雅观。独立于AuthService。