1

I am trying to send a token in a header on every request. Firstly I tried to do more complicated things, like saving in local storage but now, here I am trying to add a damn header with a token string random and it's still not add the header. Why? This is the app.js, I let here only this:

'use strict';

function testInterceptor() {
 return {
    request: function(config) {
        config.headers['x-token'] = 'lalalalala';
        console.log(config);
        console.log("Aaa");
        return config;
    }
  }
}

var app = angular.module('myapp', ['oc.lazyLoad', 'LocalStorageModule', 'oc.lazyLoad', 'ngRoute', 'naif.base64', 'ui.router', 'ngAnimate', 'ui.bootstrap']);

app.factory('testInterceptor', testInterceptor);
app.config(function($httpProvider) {
    $httpProvider.interceptors.push('testInterceptor');
});

In console.log it doesn't even printed anything. I also commented the authentication providers and let:

Default:
   Anonymous: ~

in security.yml

And when I try with postman or in browser to call my app, it doesn't add the header. Why am I doing wrong?

IleNea
  • 569
  • 4
  • 17

1 Answers1

0

Try the beow code & this is the way to send/append the token in Authorization header & then decode/check this header on server.

function testInterceptor() {
 return {
    request: function(config) {
         config.headers.Authorization = 'Bearer ' + 'lalalalala';
         return config;
    }
  }
}
Abhay
  • 6,410
  • 4
  • 26
  • 34
  • Well, still nothing. When trying with postman nothing changes and I'm not sure where should I look when I send it from browser. In $_SERVER? – IleNea Jun 14 '17 at 09:22
  • Can you paste your code or do you have include all files – Abhay Jun 14 '17 at 09:24
  • What code? This is all the code I used for this part. And I don't want to use Bearer,but can you please explain me what is not correct in the first place? – IleNea Jun 14 '17 at 09:27
  • Have you included the configuration file because as you are saying that nothing is print, so may be it happens & check whether code is running in request block or not. print anything anonymously. like console.log("Hello"); – Abhay Jun 14 '17 at 09:29
  • Because what i think, your code not running Otherwise it should give something like undefined – Abhay Jun 14 '17 at 09:30
  • And this authorization header, from where it this? I ve got something HTTP_AUTHORIZATION = Basic 352gddhsjq72=, when debugging an url configured in apache for using some authentication. There sould be replaced or where? – IleNea Jun 14 '17 at 09:31
  • Which browser r u using, is it firefox? – Abhay Jun 14 '17 at 09:45
  • So that's the problem, you have n't seen the console.log. check this : https://stackoverflow.com/questions/41933743/console-log-doesnt-work-anymore-in-firebug-since-firefox-51-0-1 – Abhay Jun 14 '17 at 09:47
  • And also, i will recommend you to use chrome while developing as per my experience, it have great debugger to work with. But it just recommendation. – Abhay Jun 14 '17 at 09:48
  • Thank you very much – IleNea Jun 14 '17 at 11:32