Pass Token by header to each Angularjs request (Authorization )

Asked

Viewed 577 times

0

I have the file interface-Factory.js which is my factory

app.factory('interfaceAPI', function ($http) {

var _getInterface = function () {
    return $http.get("/api/interfaceapi/getall");
};

var _postInterface = function (objeto) {
    return $http.post("/api/interfaceapi/post", objeto);
};

var _deleteInterface = function (Id) {
    return $http.delete("/api/interfaceapi/delete/" + Id);
};


return {
    getInterface: _getInterface,
    postInterface: _postInterface,
    deleteInterface: _deleteInterface
};

});

the token is already being generated and being saved from localStorage, I would like to know how to pass the token in the header of each request.

2 answers

0

To set the header authorization just do the following:

$http.defaults.headers.common['Authorization'] = 'Basic ' + token;

If this is the case, simply exchange 'Basic' for 'Bearer' or leave only the token, depending on the authentication method you are using.

EDIT:

The header must be set before the request is sent. For example:

var _getInterface = function () {
    var myToken = JSON.parse(localStorage.getItem('token'));
    $http.defaults.headers.common['Authorization'] = myToken.token_type + myToken.access_token;
    return $http.get("/api/interfaceapi/getall");
};

Assuming myToken.token_type is the token type and the myToken.access_token is the token itself.

  • var myToken = JSON.parse(localStorage.getItem('token'));
$http.defaults.headers.common['Authorization'] = myToken.token_type + myToken.access_token;

Desta forma?

  • @Jeffersonsouza edited the answer according to the code you reported. I hope it solves your problem!

  • this way did not work.

  • Error appeared in the console or simply did not set the header?

  • did not set the header

  • Return $http.get("/api/interfaceapi/getall"), config; then I can get into the page I want, but I know it’s not the right one. config is my variable where it receives token type and token

  • gave a console.log to see if the values are being picked up, and are

  • For the sake of conscience, do a test using: $http.defaults.headers.common.Authorization = ...

  • together with the code you sent?

  • Yes. In this case, replace the third line of the example I quoted $http.defaults.headers.common['Authorization'] = myToken.token_type + myToken.access_token; for $http.defaults.headers.common.Authorization = myToken.token_type + myToken.access_token;

Show 5 more comments

0


The solution to the problem is you:

var myToken = JSON.parse(localStorage.getItem('token'));
$http.defaults.headers.common['Authorization'] = myToken.token_type + ' ' + myToken.access_token;

simply place the code above the:

var _getInterface = function () {

    return $http.get("/api/interfaceapi/getall");
};

var _postInterface = function (objeto) {
    return $http.post("/api/interfaceapi/post", objeto);
};

var _deleteInterface = function (Id) {
    return $http.delete("/api/interfaceapi/delete/" + Id);
};


return {
    getInterface: _getInterface,
    postInterface: _postInterface,
    deleteInterface: _deleteInterface
};

});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.