[Answered ]-How can I authenticate with tokens in django rest framework

2๐Ÿ‘

โœ…

You must pass the token as you Authorization header in every request where you access logged in content. You can do this by using $https default headers.

   $http
   .post('/api-token-auth/', {email: $scope.account.email, password: $scope.account.password})
   .then(function(response) {
    ...
    } else {
        localStorage.setItem('myApp.token',response.token);
        $http.defaults.headers.common.Authorization = 'Bearer ' + response.token
        $state.go('dashboard');
    }
    ...

You should also save this value in localstorage and set the default header in you apps run, so that when a user comes back yo your page they are already logged in. Like this:

angular.module('myApp').run(function($http) {
    $http.defaults.headers.common.Authorization = 'Bearer ' + localStorage.getItem('myApp.token')
});
๐Ÿ‘คErik Svedin

0๐Ÿ‘

If you receive a token as a response, it means that the user is already logged in.

To use the token, you should add it as an http header for subsequent requests.

Authorization: JWT <your_token>
๐Ÿ‘คRod Xavier

Leave a comment