0

I'm trying to use Django REST Framework's token-based authentication scheme with an AngularJS client. I'm able to successfully retrieve and store a token from the server, but I'm having trouble figuring out how to attach the token to subsequent responses. Here's the service that manages logging in and saving a token:

angular.module('mymodule')
.factory('loginService', function ($http, $window) {

    var api_base = "link to my api";

    return {
        async: function() {
            return $http.get(api_base + "authentication/login/").then(function (response) {
                return response.data;
            }, function errorCallback(response) {
                console.log("Testuser API Error: " + response);
                return null;
            });
        },

        loginUser: function(email, password) {
            self.saveToken = function(auth_token) {
                $window.localStorage['jwtToken'] = auth_token;
            };
            self.getToken = function() {
                return $window.localStorage['jwtToken'];
            };

            console.log("...to listing " + email);

            return $http.post("link to my api/authentication/login/", {
                email: email,
                password: password


            }).then(function(response) {
                if(response.config.url.indexOf("link to my api") === 0 && response.data.auth_token) {
                    self.saveToken(response.data.auth_token);
                }

                return response;


            });

        }



    };

});

Here's the controller associated with the above service to handle logging in:

angular.module('mymodule').controller("LoginController", function(loginService, $scope) {
$scope.loginusers = [];

$scope.refresh = function() {
    loginService.async().then(function(data) {
        if (data == null)
            return;

        console.log(data[0]["email"]);

        $scope.loginusers = [];
        for (var loginuser in data)
            $scope.loginusers.push(loginuser);
    });
};
$scope.refresh();


// Test //
$scope.loginTestUser = function() {
    console.log("something...");

    loginService.loginUser(
        $scope.email,
        $scope.password


    )
};

//////



});

And here's the service that I'd like to use for displaying a user's profile after the token is sent back to the server.

angular.module('mymodule').factory("profileService", function($http, loginService, $httpProvider) {

var api_base = "link to my api";


$httpProvider.interceptors.push(function($q, $window) {
    return {
        'request': function(config) {

            config.headers['Token'] = $window.localStorage['jwtToken'];
            return config;
        }
    };
});

return {


    async: function() {
        return $http.get(api_base + "authentication/me/").then(function (response) {
            return response.data[0];
        }, function errorCallback(response) {
            console.log("Profile API Error: " + response);
            return null;

        });

    }

};



});

How should I be approaching this?

Jeff
  • 19
  • 3

0 Answers0