Skip to content

This is a complete backend API based application which contains Custom User Model, User Registration API, User Authentication/Login API, Get User Profile API, Send Password Reset Email API, Change Password API.

Notifications You must be signed in to change notification settings

Pushpendra9350/DRF-Authentication-API-with-JWT

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Complete Authentication API with JWT with Django Rest Framework

Features

  • Custom User Model
  • User Registration API
  • User Authentication/Login API
  • Get User Profile API
  • Send Password Reset Email API
  • Change Password API

Intall all required packages

pip install django
pip install djangorestframework
pip install djangorestframework-simplejwt
pip install django-cors-headers

Configuration

In settings.py file

INSTALLED_APPS = [
    ...
    'rest_framework_simplejwt',
    'rest_framework',
    'corsheaders'
]

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


MIDDLEWARE = [
    ...,
    "corsheaders.middleware.CorsMiddleware",
    "django.middleware.common.CommonMiddleware",
    ...,
]


CORS_ALLOWED_ORIGINS = [
    "https://example.com",
    "https://sub.example.com",
    "http://localhost:8080",
    "http://127.0.0.1:9000",
]


# https://django-rest-framework-simplejwt.readthedocs.io/en/latest/settings.html
from datetime import timedelta
...

SIMPLE_JWT = {
    'ACCESS_TOKEN_LIFETIME': timedelta(minutes=5),
    'REFRESH_TOKEN_LIFETIME': timedelta(days=1),
    'ROTATE_REFRESH_TOKENS': False,
    'BLACKLIST_AFTER_ROTATION': False,
    'UPDATE_LAST_LOGIN': False,

    'ALGORITHM': 'HS256',
    'SIGNING_KEY': SECRET_KEY,
    'VERIFYING_KEY': None,
    'AUDIENCE': None,
    'ISSUER': None,
    'JWK_URL': None,
    'LEEWAY': 0,

    'AUTH_HEADER_TYPES': ('Bearer',),
    'AUTH_HEADER_NAME': 'HTTP_AUTHORIZATION',
    'USER_ID_FIELD': 'id',
    'USER_ID_CLAIM': 'user_id',
    'USER_AUTHENTICATION_RULE': 'rest_framework_simplejwt.authentication.default_user_authentication_rule',

    'AUTH_TOKEN_CLASSES': ('rest_framework_simplejwt.tokens.AccessToken',),
    'TOKEN_TYPE_CLAIM': 'token_type',
    'TOKEN_USER_CLASS': 'rest_framework_simplejwt.models.TokenUser',

    'JTI_CLAIM': 'jti',

    'SLIDING_TOKEN_REFRESH_EXP_CLAIM': 'refresh_exp',
    'SLIDING_TOKEN_LIFETIME': timedelta(minutes=5),
    'SLIDING_TOKEN_REFRESH_LIFETIME': timedelta(days=1),
}


# Email Configuration
EMAIL_BACKEND="django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = os.environ.get('EMAIL_USER')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASS')
EMAIL_USE_TLS = True

#Password reset token timeout
PASSWORD_RESET_TIMEOUT = 300 # 5 minutes

Important parameters ACCESS_TOKEN_LIFETIME: Access token lifetime(How much time you access token will live) REFRESH_TOKEN_LIFETIME: Refresh token lifetime(How much time you refresh token will live)

In root urls.py file

from rest_framework_simplejwt.views import (
    TokenObtainPairView,
    TokenRefreshView,
)

urlpatterns = [
    ...
    path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
    ...
]

# If you want to verify token use (Optional) 
from rest_framework_simplejwt.views import TokenVerifyView

urlpatterns = [
    ...
    path('api/token/verify/', TokenVerifyView.as_view(), name='token_verify'),
    ...
]
  • TokenObtainPairView: Will give you a pair of tokens one is access token and another one is refersh token

    • Access token(Short lived token): Generally, It has less life then refresh token. it is used to access projected api's.
    • Refresh token(Long lived token): Generally, It has more life then access token. it is used to generate expired access token again.
  • TokenRefreshView: Will give you an access token with the help og refresh token which is expired.

  • TokenVerifyView: Will verify your token is it a valid token or not.

Creating tokens manually

from rest_framework_simplejwt.tokens import RefreshToken

def get_tokens_for_user(user):
    refresh = RefreshToken.for_user(user)

    return {
        'refresh': str(refresh),
        'access': str(refresh.access_token),
    }

About

This is a complete backend API based application which contains Custom User Model, User Registration API, User Authentication/Login API, Get User Profile API, Send Password Reset Email API, Change Password API.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages