3

I'm using DRF and Angular, which client environment is a mobile devices.

I've found out a django-rest-auth package.

I haven't hesitate to choice for that, because that provides a TokenAuthentication feature, which is suitable with a mobile client.

When I sent a login request, client receives a token.

enter image description here Then, I was add a the bellow in request success callback.

 login: function(username, password) {
        return $http.post('http://192.168.0.3:8000/rest-auth/login/', {
            'username':username,
            'password':password,
        }).success(function(data) {
            $http.defaults.headers.common.Authorization = 'Token ' + data.key;
            Account.authenticated = true;
            console.log("login success", data)
        })

At server's console, output about incoming request is the bellow

'HTTP_AUTHORIZATION': 'Token 3fae470d169adb550e538c99a754fcfbe3485f75'

But, I saw an unexpected result, like this:

request.user AnonymousUser
request.auth None

According to here, If I send a request with token, which extra authentication works will be processed by itself.

Should I add an other code for complete authentication?

(ex. register a token into django's session storage.)

I would like to hear your advice.

1 Answer 1

3

I solved for a problem, which cause is just stupid mistakes

I didn't look carefully at the reference documents.

To use the TokenAuthentication scheme you'll need to configure the authentication classes to include TokenAuthentication, and additionally include rest_framework.authtoken in your INSTALLED_APPS setting:

So I had added the configuration in settings.py.

 REST_FRAMEWORK = {
     'DEFAULT_AUTHENTICATION_CLASSES': (
         'rest_framework.authentication.BasicAuthentication',
         'rest_framework.authentication.SessionAuthentication',

         # I add this config
         'rest_framework.authentication.TokenAuthentication',
     )
}

After send a login request terminal to server, then If I request with the "GET", terminal console outputs like the below.

request.user admin

request.auth 626ba4b1357cb472fc4bb0c58afb026cf21dd175

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.