mohammadjh
mohammadjh

Reputation: 722

How to get user info after successful authentication with django-rest-framework-jwt in angularjs

I'm working on a project with django and angularjs where I need to use djangorestframework-jwt for authentication. I'm new to djangorestframework-jwt. I use '/api-token-auth/' for login, but I don't get any user information.

My authentication services is:

function login (username, password, callback) {
        return $http.post('/api-token-auth/', {
            username: username, password: password
        }).then(loginSuccessFn(callback), loginErrorFn);

        function loginSuccessFn (callback) {
            return function (data, status, headers, config, response) {
                localStorage.setItem('empee.token',data.data.token);
                $http.defaults.headers.common['Authorization'] = 'Bearer ' + data.data.token;
                if (typeof callback !== 'undefined') {
                    callback();
                }
            }

        }

        function loginErrorFn (data, status, headers, config) {
            console.error('Epic failure!');
        }
    }

Login controller:

function login() {
        Authentication.login(vm.username, vm.password, function() {
            $state.go('dashboard');
        });
    }

Upvotes: 0

Views: 2943

Answers (1)

Karthik RP
Karthik RP

Reputation: 1078

Considering you have User URL exposed in Django-rest-framework, you can do a $http.get.

$http.get('/user-url/'+userid).then(function(response){
    if(response.status === 200){
        user_obj = response.data;
    }
});

To get the userId, All you would need to user angular-jwt

userid = jwtHelper.decodeToken(token).user_id;

Upvotes: 2

Related Questions