AngularJS “localStorage.getItem” is null in $resource

前端 未结 1 1237
死守一世寂寞
死守一世寂寞 2021-01-19 05:40

I have an angular $resource for login and getting user\'s info. The login sends the username and password to server and gets a Bearer token. In the succes

相关标签:
1条回答
  • 2021-01-19 06:19

    Your problem is that the resource definition is provided at the time of creation (before you have saved a token).

    You can either add a general request interceptor to the $httpProvider so all requests are covered or change your getEmail action's header to use a function so it is evaluated at run time, eg

    headers: {
        'Authorization': function() {
            var token = $window.localStorage.getItem('TK');
            return token ? 'Bearer ' + token : null;
        }
    }
    
    0 讨论(0)
提交回复
热议问题