Get $ state.current (ui-router) into the $ http interceptor

What am i trying to achieve

I would like to get the value of the current state data in my $ http interceptor. I already created an interceptor.

Problem

I don't know how to catch $ state.current.data in my interceptor. I am trying to use $ injector.get ($ state) but cannot read the object property.

 
 angular.module('mymodule').factory 'HttpInterceptor', [
 '$location', '$injector'
  ($location, $injector) ->

  request: (config) ->
   # Do something when sending the request
   console.log(config.headers.hasOwnProperty('Authorization'))
   if config.headers.hasOwnProperty('Authorization') == false
    $location.url("/login")
   config
+4
source share
3 answers

I could give you an idea of ​​how you can achieve this through the execution unit.

app.run(function (StateInterceptor, FPSSO, HTTPResponseCode, $state, SessionManager, notifications, Utils, $timeout) {
  StateInterceptor.startService();

  var httpSecurityInterceptor =  function(response, deferred){
    var propagatePromise = true;
    switch(response.status){
      case HTTPResponseCode.UNAUTHORIZED:
        SessionManager.Logout();
        notifications.showErrorMessage("Your session has expired. Please sign in again.", "Error");
        $state.go('Logout', {isLogout: true, returnUrl: $state.current.name});  
        propagatePromise = false;  
        break;
      default:
        var res = Utils.ExtractResponse(response);
        res.status = response.status;
        deferred.reject(res);
    }
    return propagatePromise;
  }

  FPSSO.API.setErrorInterceptor(httpSecurityInterceptor);

, switch $state.current "". , , , .

- .

0

$state, , .

0

I can correctly enter $ state using the following code in my interceptor:

   $timeout(function () {
                    $state = $injector.get('$state');
                });
0
source

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


All Articles