Set RestAngular default query parameters after login

I am running an API that requires all authenticated actions to include an authentication token in the request, however I do not have an authentication token until I log in.

I saw only examples of setting default query parameters in Restangular in app.config . Is it possible to set this until the user logs in and User.auth_token is set?

So basically instead of:

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

I need:

 app.config(function(RestangularProvider) { RestangularProvider.setDefaultRequestParams({ auth_token: User.auth_token }); }); 
+6
source share
7 answers

I know this is an old thread, but this SO question kept popping up when I was Googling (yes, I just used Google as a verb ... handle this: P) for resolution, so I thought I should provide my solution . Hope this helps the OP or anyone else who may come across this page.

 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; } ]); 
+2
source

Why would you set the token as part of the response against the header? In this way.

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

In my case, I use

 Restangular.setDefaultRequestParams({token: localstorage.get('token')}); 

It works with me. Please see my fragment here. https://github.com/fugokidi/ng-snippets/blob/master/rest.js

+1
source

If you want to do something like this, you need to remove your code from app.cofig and go to it when you find that the user is logged in.

You can set defaultRestParams for restangular anywhere in the application using the Restangular service.

See https://github.com/mgonto/restangular#setdefaultrequestparams for more information.

0
source

More Angular is an example from a project I was working on:

 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) 
0
source

The following code will read the token from the repository for each request.

 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'); } }); }); 
0
source

I also struggled with this. Instead of using

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

try using

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

Source: https://habr.com/ru/post/972611/


All Articles