[Django]-How to authenticate request in Django-rest-auth?

3๐Ÿ‘

โœ…

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");
    },
});
๐Ÿ‘คilse2005

1๐Ÿ‘

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.

๐Ÿ‘คSuReSh

Leave a comment