0

Hi I am using django rest-framework, i want to implement token base authentication, i am using basic rest-framework token authentication module.

but it return same token on every request. ex(87d97bb2df56e39c12b38131087bcfd232720d9a), i am getting this string on every request i sent to my server.

my setting.py file

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'restApp2', # Local Apps (My project's apps)
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
]

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
    'rest_framework.authentication.TokenAuthentication',  # <-- And here
],
}

urls.py file

from django.contrib import admin
from django.urls import path, include
from restApp2 import views
from rest_framework.authtoken.views import obtain_auth_token

urlpatterns = [
    path('admin/', admin.site.urls),
    path('hello/', views.HelloView.as_view(), name='hello'),
    path('api-token-auth/', obtain_auth_token, name='api_token_auth'),  # <-- And here
    ]

urlpatterns += [
    path('accounts/', include('django.contrib.auth.urls')),
]

i am calling bellow url using post method from POSTMON.

POST http://localhost:7070/rest-auth/login/

and in response i get

87d97bb2df56e39c12b38131087bcfd232720d9a.

but i want different token on new request.

please help me, Thank You

Sagar T
  • 89
  • 1
  • 1
  • 11
  • try this one: https://stackoverflow.com/questions/27570377/change-token-for-tokenauthentication-each-time-user-logs-in – Shoaib Zafar Jan 02 '19 at 12:26

1 Answers1

2

If you check obtain_auth_token view, its either getting existing token or creating new one if there is no token in the database for the user.

If want new token on each login, then delete the previous tokens after logout. django rest framework - token authentication logout

Nagesh Dhope
  • 797
  • 5
  • 13