Search code examples
djangodjango-rest-frameworkdjango-rest-authdjango-rest-framework-simplejwt

What is the purpose of model Token in the admin section?


I am fairly new to the rest api. I am trying to use dj-rest-auth package with simple-jwt for auth handling. Everything works fine in my project. The registration/login etc. But in my django admin site there is a model registered Token which is every time empty. What is the purpose of this model Token? How tokens are managed with dj-rest-auth and simple jwt package ?

settings.py

installed_apps= [
..
    'rest_framework',
    'rest_framework.authtoken',
    'dj_rest_auth',
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'dj_rest_auth.registration',



REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    )

}

REST_USE_JWT = True
SIMPLE_JWT = {
    'ACCESS_TOKEN_LIFETIME': timedelta(minutes=5),
    'REFRESH_TOKEN_LIFETIME': timedelta(days=7),
}


ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = "mandatory"
ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS = 1
ACCOUNT_AUTHENTICATION_METHOD = "username"
ACCOUNT_LOGIN_ATTEMPTS_LIMIT = None

urls.py

path('user/', include('dj_rest_auth.urls')),
path('user/register/', include('dj_rest_auth.registration.urls')),
path('confirm/email/', CustomVerifyEmailView.as_view(), name='account_email_verification_sent'),

Solution

  • You have Token model in admin because you added rest_framework.authtoken to your installed apps. This model is for basic token (stored in db) authentication: https://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication

    JWT (JSON Web Tokens) tokens are stateless and are not stored in db. If you want to read more about JWT I recommend: https://jwt.io/introduction