1

I am using Laravel as backend and JWT library (https://github.com/tymondesigns/jwt-auth ) integrated in it. In the backend, on the every request the laravel responds with new token attached in response header like

Authorization: Bearer <token>

here i am implementing front end in angular js Framework, and i intercept every request using a interceptor,

 angular.module('Authentication')
.factory('httpRequestInterceptor',['$cookieStore','$location',
 function($cookieStore,$location) {
   return {
        request: function(config) {
            var currenUser = $cookieStore.get("globals");
            if(currenUser) 
            {
                var token = currenUser.currentUser.token;
                if(token)
                {
                    config.headers['Authorization'] = 'Bearer ' + token;
                }
            }else{
                $location.path('/login');
            }
            console.log(config);
            return config;
        } ,

        'response': function(response) {

            console.log(response.headers()); // no authorization header i get, 
            //but when i console, in the network i can see the authorization header token 
            //here i have to access the response header 
            //but i can't get authorization bearer from header 
            //if i get the value of token i can replace the currant cookie token value with this response token
            console.log(response);
            return response;
        }


    }
}]);

php laravel 5.4 web.php

Route::group(['middleware' => ['jwt.auth', 'jwt.refresh']], function() {

    Route::get('profile', [
       'as' => 'auth.getAuthenticatedUser',
       'uses' => 'AuthController@getAuthenticatedUser'
    ]);

    Route::post('logout', 'Api\AuthController@logout');

});

In first time the token is pass from jwt library in body part. It is stored in cookies, in the subsequent request the token passes via Header.

The server responds back to client with generated token via header,after that i have to replace the cookie token value.

Here is the problem i get, i can't access the response header information by the interceptor response. How can i achieve it? Any response is appreciated. Thanks.

Phil
  • 157,677
  • 23
  • 242
  • 245
Jees K Denny
  • 531
  • 5
  • 27
  • can you provide fiddle? because you should be able to do that. – MehulJoshi Jul 06 '17 at 06:05
  • *"on the every request the laravel responds with new token attached in response header"* <- that doesn't sound right at all. The bearer token should be negotiated between the client and server then used by the client in requests until the token expires – Phil Jul 06 '17 at 06:07
  • https://github.com/tymondesigns/jwt-auth/wiki/Authentication please check in the last section , i am used refresh token middileware, so in every request the authorization bearer responds with new token. @Phil – Jees K Denny Jul 06 '17 at 06:10
  • Ah, ok. A single-use token workflow – Phil Jul 06 '17 at 06:14
  • yes, could you please tell me how to access the header authorization in angular js? @Phil – Jees K Denny Jul 06 '17 at 06:17
  • So in your response interceptor, `console.log('Response auth header', response.headers('Authorization'))` doesn't yield anything? – Phil Jul 06 '17 at 06:20
  • nothing, i checkd it that also, on every request i consoled, there is no authorization header information, when postman responds nicely, and in the console networks, the headers received correctly. @Phil – Jees K Denny Jul 06 '17 at 06:22
  • Are these cross-domain requests (CORS)? – Phil Jul 06 '17 at 06:29
  • all are running in localhost now. means in same domain.But both are clearly separated, front end and back end. @Phil – Jees K Denny Jul 06 '17 at 06:31
  • 1
    Are the front and back ends running on separate ports or anything like that? I only ask as this looks exactly like your problem ~ https://github.com/tymondesigns/jwt-auth/issues/11#issuecomment-110681813. Maybe you need to configure the `Access-Control-Expose-Headers` even if it's not cross-domain – Phil Jul 06 '17 at 06:37
  • yes! the laravel works in different port. and front end work in different port. – Jees K Denny Jul 06 '17 at 06:41
  • Different ports means requests are cross-domain. Seems you might have an answer now :) – Phil Jul 06 '17 at 06:42

0 Answers0