3
votes

I am trying to integrate django-rest-auth package in my web application. So far i am able to register users, send password reset email and login using the API provided by django-rest-auth package.

Now when i send a login request, it returns "token" upon successful authentication.

How do i send authentication token in further requests? For example, i am trying to fetch user using GET /rest-auth/user but it is giving me a response Authentication credentials not provided. I have tried passing token via HTTP Basic Authentication (base64 encode token as username and leave password as empty). I am still not able to work.

Anyone knows how i am supposed to pass this token in requests?

2

2 Answers

3
votes

If you want to use the Token Authentication you have to set the Authorization HTTP header. From the docs:

For clients to authenticate, the token key should be included in the Authorization HTTP header. The key should be prefixed by the string literal "Token", with whitespace separating the two strings. For example:

Authorization: Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b

In an ajax call you can a header like this:

$.ajax({
    type: 'POST',
    url: url,
    beforeSend: function (request)
    {
       request.setRequestHeader("Authorization", "Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b");
    },
});
1
votes

HI You need to send token in headers

enter image description here

$.ajax({
            type:"POST",
            beforeSend: function (request)
            {
                request.setRequestHeader("Authority", 'Bearer 33a95862173cc0565fe502eb062b2e7c67e23a3a');
            },

and in django code use

user = request.user
if user:
 return "User token verified"
elif :
 return "User token not verified"

in django automaticaly find token in headers and using token fetch user data.