我正在点击一个API,该API要求所有经过身份验证的操作都在请求中包含身份验证令牌,但是,在我登录之前,我没有身份验证令牌。
我在app.config
中只看到了在Restangular中设置默认请求参数的示例。
是否可以在用户登录并设置User.auth_token
之后设置此项?
所以基本上代替:
app.config(function(RestangularProvider) {
RestangularProvider.setDefaultRequestParams({
auth_token: 'thisistheauthenticationtoken'
});
});
我需要:
app.config(function(RestangularProvider) {
RestangularProvider.setDefaultRequestParams({
auth_token: User.auth_token
});
});
答案 0 :(得分:2)
我知道这是一个老线程,但是这个问题一直出现在我谷歌搜索时(是的,我只是用谷歌作为动词......处理它: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;
}
]);
答案 1 :(得分:2)
为什么要将令牌设置为响应的一部分而不是标头?像这样。
Restangular.setDefaultHeaders({ authentication: 'bearer ' + token.authentication });
答案 2 :(得分:1)
在我的情况下,我使用
Restangular.setDefaultRequestParams({token: localstorage.get('token')});
这适用于我。请在这里查看我的代码段。 https://github.com/fugokidi/ng-snippets/blob/master/rest.js
答案 3 :(得分:0)
如果您想执行此类操作,则需要从app.cofig中删除代码,然后在找到用户登录时移至。
您可以使用Restangular服务在任何应用程序点为restangular设置defaultRestParams。
有关详细信息,请参阅https://github.com/mgonto/restangular#setdefaultrequestparams。
答案 4 :(得分:0)
我正在研究的项目中的一个更具角度的例子:
angular.module('app', [ 'restangular' ])
.factory('API', function(Restangular){
return Restangular.withConfig(function(config){
config
.setBaseUrl('https://api.example.com')
// etc etc etc
; // END config
});
})
.factory('Auth', function(API){
return {
login: function(credentials){
// Assuming I just POST /session/new to get an OAuth token,
// which is totally not a thing that OAuth should do.
API.one('session').post('new', credentials)
.then(function(auth){ // Assuming `auth = { access_token: '...' }`
API.setDefaultHeaders({
Authorization: 'bearer ' + auth.access_token
// Assuming OAuth Bearer Token
});
})
},
logout: function(){ /* . . . */ }
};
})
.controller('MainController', function(API, Auth){
var self = this;
self.user = { };
this.login = function(credentials){
Auth.login(credentials).then(function(){
self.user = API.one('user').$object;
});
});
})
; // END module(app)
答案 5 :(得分:0)
以下代码将为每个请求从存储中读取令牌。
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');
}
});
});
答案 6 :(得分:0)
我也很挣扎。 而不是使用
h
尝试使用
RestangularProvider.setDefaultRequestParams({
auth_token: 'thisistheauthenticationtoken'
});