auth0 / angular-jwt

Library to help you work with JWTs on AngularJS
MIT License
0 stars 1 forks source link

Help | Traditional + Angular JWT #150

Closed GlauberF closed 4 years ago

GlauberF commented 7 years ago

Hello I have the following code in angularjs, a conventional interveiver, but my jwt that comes from the API, now contains information that I need to decode to use in APP.

How could I rewrite the following code to use the JWT Angular library ?? If anyone can rewrite and post here, I will be eternally grateful :)

`(function(){ 'use strict';

angular
    .module('vimbo')
    .factory('tokenInterceptor', tokenInterceptor)

function tokenInterceptor($window, $q, $location){

    var interceptor = {};

    // armazena token
        interceptor.response = function(response){
            console.log('chamei interceptor');
            console.log(response);
            var token = response.headers('x-access-token');
            if(token){
                // window.localStorage.setItem('token', token);
                $window.localStorage.token = token;
                console.log("armazenado token");
            }
            return response;
        };

    // injeta a cada request o token
        interceptor.request = function(config){
            config.headers = config.headers || {};
            if($window.localStorage.token){
                // config.headers['x-access-token'] = $window.localStorage.token;
                config.headers.Authorization = 'Bearer ' + $window.localStorage.token;
                console.log('adicionando token');
            }
            return config;
        };

    // tratando erro 401
        interceptor.responseError = function(rejection){
            if(rejection != null && rejection.status == 401){
                delete $window.localStorage.token;
                $location.path('/auth/login');
            }
            return $q.reject(rejection);
        };

    return interceptor;
}

}());`

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you have not received a response for our team (apologies for the delay) and this is still a blocker, please reply with additional information or just a ping. Thank you for your contribution! 🙇‍♂️