Angular frontend authentication example using Json Web Token (JWT) with Django as backend
Repo contains Django code as backend and angular as frontend and authentication system using Django REST framework JWT
To use this repo clone it and in root folder open the command prompt and run
pipenv install
python manage.py migrate
python manage.py runserver
-
Create virtualenv using pipenv
# install pipenv if not installed or you can use any other virtualenv package of your choice pip install pipenv pipenv install django djangorestframework djangorestframework-jwt python-decouple
-
Activate virtualenv using
pipenv shell
-
Start Django project
django-admin startproject backend . python manage.py migrate python manage.py createsuperuser python manage.py startapp microblog
-
Configure Django project settings by adding rest framework and jwt and python-decouple
First add support for env variables by creating a
.env
file and addPROJECT_KEY=<your_secret_key>
import os import datetime from decouple import config ... SECRET_KEY = config('PROJECT_KEY') # <- move secret key from here to .env file
Now add rest framework settings
INSTALLED_APPS = [ ... 'rest_framework', ] ... # add at the bottom REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.IsAuthenticatedOrReadOnly', ), 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_jwt.authentication.JSONWebTokenAuthentication', 'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.BasicAuthentication', ), } ... JWT_AUTH = { 'JWT_ALLOW_REFRESH': True, 'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=3600), }
-
Start django app for blogs
python manage.py startapp microblog
And add it in
INSTALLED_APPS