Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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_Restangular - Fatal编程技术网

Angularjs 登录后设置重新启动的默认请求参数

Angularjs 登录后设置重新启动的默认请求参数,angularjs,restangular,Angularjs,Restangular,我遇到了一个API,它要求所有经过身份验证的操作都在请求中包含一个身份验证令牌,但是,在我登录之前,我没有身份验证令牌 我在app.config中只看到在restanglar中设置默认请求参数的示例。 在用户登录并设置了user.auth\u令牌之前,是否可以设置此项 因此,基本上不是: app.config(function(RestangularProvider) { RestangularProvider.setDefaultRequestParams({ auth

我遇到了一个API,它要求所有经过身份验证的操作都在请求中包含一个身份验证令牌,但是,在我登录之前,我没有身份验证令牌

我在
app.config
中只看到在restanglar中设置默认请求参数的示例。 在用户登录并设置了
user.auth\u令牌之前,是否可以设置此项

因此,基本上不是:

app.config(function(RestangularProvider) {
    RestangularProvider.setDefaultRequestParams({
        auth_token: 'thisistheauthenticationtoken'
    });
});
我需要:

app.config(function(RestangularProvider) {
    RestangularProvider.setDefaultRequestParams({
        auth_token: User.auth_token
    });
});

若要执行类似操作,则需要从app.cofig中删除代码,并在发现用户已登录时移动到

您可以使用Restanglar服务在应用程序的任何位置为Restanglar设置defaultRestParams


有关更多信息,请参阅

我知道这是一条老线索,但当我在谷歌搜索时(是的,我只是用谷歌作为动词…处理它:p)这个问题不断出现,所以我想我应该提供我的解决方案。希望这将有助于OP或任何其他人可能会看到这一页

angular.module("app").factory("UserService", [
    "$rootScope",
    "$state",
    "$q",
    "Restangular",
    function ($rootScope, $state, $q, Restangular) {
        var UserSvc = {};
        var Identity;

        /*
            This creates a scoped copy of Restangular
            Normally this is where you would use setDefaultRequestParams,
            but it would only affect this scope and not ALL API requests in your app
         */
        var UsersAPI = Restangular.withConfig(function (RestangularConfigurer) {
            RestangularConfigurer.setBaseUrl("api/1.0/users");
        });

        UserSvc.login = function (credentials) {
            var $defer = $q.defer();

            UsersAPI.all("start-session").post(credentials).then(function(respData){
                if (respData.apikey) {
                    Identity = respData.plain();

                    /*
                        User is authenticated and API key is obtained from server response

                        Note how I do NOT use the setDefaultRequestParams function:
                        If we do the withConfig/setDefaultRequestParams, it only affects local scope, not global
                        This method modifies the ROOT Restangular object and
                        will then propegate through all future use of Restangular in your app
                     */
                    Restangular.configuration.defaultRequestParams.common.apikey = Identity.apikey;


                    if ($rootScope.toState && $rootScope.toState.name != "login") {
                        $state.go($rootScope.toState.name, $rootScope.toStateParams || {});
                    } else {
                        $state.go("app.dashboard");
                    }

                    $defer.resolve(Identity);
                }
                else {
                    Identity = undefined;

                    $defer.reject(Identity);
                }
            },function (respData) {
                $defer.reject(respData);
            });

            return $defer.promise;

        };

        return UserSvc;
    }
]);

为什么要将令牌设置为响应的一部分,而不是在头中?像这样

Restangular.setDefaultHeaders({ authentication: 'bearer ' + token.authentication });

我一直在做的一个项目中有一个更具角度感的例子:

angular.module('app',['restanglar']))
.factory('API',函数(重新启动){
返回Restangular.withConfig(函数(配置){
配置
.setBaseUrl('https://api.example.com')
//等等等等等等
;//结束配置
});
})
.factory('Auth',函数(API){
返回{
登录:功能(凭证){
//假设我只是POST/session/new来获取OAuth令牌,
//这完全不是OAuth应该做的事情。
API.one(‘会话’).post(‘新’,凭证)
.then(函数(auth){//假设'auth={access_token:'.'.''}`
API.setDefaultHeaders({
授权:“承载人”+auth.access\u令牌
//假定OAuth承载令牌
});
})
},
注销:函数(){/*…*/}
};
})
.controller('MainController',函数(API,Auth){
var self=这个;
self.user={};
this.login=函数(凭据){
Auth.login(凭证)。然后(函数(){
self.user=API.one('user').$object;
});
});
})
; // 终端模块(应用程序)
在我的例子中,我使用

Restangular.setDefaultRequestParams({token: localstorage.get('token')});
这对我有用。请看一下我的代码片段。

以下代码将为每个请求从存储器中读取令牌

app.config(function(RestangularProvider) {

    //Injext $cookies manually (there might be better ways to do this)
    var $cookies;
    angular.injector(['ngCookies']).invoke(['$cookies', function(_$cookies_) {
        $cookies = _$cookies_;
    }]);

    RestangularProvider.setDefaultHeaders({
        Authorization: function() {
            return $cookies.get('token');
        }
    });
});
我也为此挣扎。 而不是使用

RestangularProvider.setDefaultRequestParams({
    auth_token: 'thisistheauthenticationtoken'
});
试用

Restangular.setDefaultRequestParams({auth_token:'thisistheauthenticationtoken'});

我尝试在
auth_令牌可用后移动这些行,但它似乎没有改变任何东西。你解决了这个问题吗?我正在处理相同的问题…有进展吗?(+1)