Using token authentication and rest framework backend
python -m venv .env
source .env/bin/activate
pip install django==3.2.9 djangorestframework djangorestframework-jwt django-cors-headers psycopg2 pytz
django-admin startproject backend
cd backend
pip freeze > requirements.txt
python manage.py startapp app_name
INSTALLED_APPS = [
# ...
'rest_framework',
'corsheaders',
]
MIDDLEWARE = [
# ...
'corsheaders.middleware.CorsMiddleware', # Note that this needs to be placed above CommonMiddleware
'django.middleware.common.CommonMiddleware', # This should already exist
# ...
]
#...
createdb tutorial_db
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'tutorial_db',
}
}
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
),
}
CORS_ORIGIN_WHITELIST = ['https://localhost:3000','http://localhost:3000']
##In project urls.py
from django.contrib import admin
from django.urls import path
from rest_framework_jwt.views import obtain_jwt_token
urlpatterns = [
path('admin/', admin.site.urls),
path('token/', obtain_jwt_token)
]
python manage.py migrate
You will now be able to recieve JWT tokens.