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.

Community
  • 1
  • 1
Haegyun Jung
  • 755
  • 1
  • 7
  • 14

1 Answers1

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

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Haegyun Jung
  • 755
  • 1
  • 7
  • 14