|
1 |
| -# drf-authentication-rest-api |
2 |
| -An Authentication REST API developed with Python, Django & Django Rest Framework (DRF) that utilizes JWT |
| 1 | +# Django REST API with JWT Authentication (Authentication API) |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +We're going to take you step-by-step to build a fully functional open-source JWT Authentication REST API using Python, Django Rest Framework. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +1. [Python==3.9.5 or Greater](https://python.org) |
| 10 | +2. [Django==4.2.6 or Greater ](https://docs.djangoproject.com) |
| 11 | +3. [djangorestframework==3.14.0](https://www.django-rest-framework.org/) |
| 12 | +4. [PyJWT==2.7.0](https://pyjwt.readthedocs.io/en/stable/) |
| 13 | + |
| 14 | +You can view all the dependencies from the [requirements.txt](./requirements.txt) file. |
| 15 | + |
| 16 | +## Project Installation Instructions |
| 17 | + |
| 18 | +1. Navigate to your workspace and create a virtual environment with `python -m venv env` |
| 19 | + |
| 20 | +2. Activate the virtual environment |
| 21 | + |
| 22 | + - **Linux**: Activate the virtual environment run: `source .env/bin/activate` |
| 23 | + |
| 24 | + - **Windows**: Activate the virtual environment run: `env\Scipts\activate` |
| 25 | + |
| 26 | +3. Install the following project dependencies run: |
| 27 | + |
| 28 | + ```bash |
| 29 | + pip install Django==4.2.6 |
| 30 | + pip install djangorestframework==3.14.0 |
| 31 | + pip install PyJWT==2.7.0 |
| 32 | + ``` |
| 33 | + |
| 34 | +4. After the installation is complete let's create our [django project](https://docs.djangoproject.com/en/4.2/intro/tutorial01/), we are going to call our project **core** run: `django-admin startproject core`. |
| 35 | +Let’s look at what startproject created: |
| 36 | + |
| 37 | + ```markdown |
| 38 | + core/ |
| 39 | + manage.py |
| 40 | + core/ |
| 41 | + __init__.py |
| 42 | + settings.py |
| 43 | + urls.py |
| 44 | + asgi.py |
| 45 | + wsgi.py |
| 46 | + ``` |
| 47 | + |
| 48 | +5. After the creation of the project, let's create an app called **users**. First you need to `cd` to the created project which is named **core** and run: `django-admin startapp users`. The application will be created inside the Django project **core**. Let's look at the folder structure below. |
| 49 | + |
| 50 | + ```markdown |
| 51 | + |
| 52 | + core/ |
| 53 | + manage.py |
| 54 | + core/ |
| 55 | + __init__.py |
| 56 | + settings.py |
| 57 | + urls.py |
| 58 | + asgi.py |
| 59 | + wsgi.py |
| 60 | + users/ |
| 61 | + __init__.py |
| 62 | + admin.py |
| 63 | + apps.py |
| 64 | + migrations/ |
| 65 | + __init__.py |
| 66 | + models.py |
| 67 | + tests.py |
| 68 | + views.py |
| 69 | + ``` |
| 70 | + |
| 71 | +6. Navigate to`settings.py` and update the following: |
| 72 | + |
| 73 | + ```python |
| 74 | + INSTALLED_APPS = [ |
| 75 | + 'django.contrib.admin', |
| 76 | + 'django.contrib.auth', |
| 77 | + 'django.contrib.contenttypes', |
| 78 | + 'django.contrib.sessions', |
| 79 | + 'django.contrib.messages', |
| 80 | + 'django.contrib.staticfiles', |
| 81 | + 'rest_framework', # NEW SET UP FOR DJANGO REST FRAMEWORK |
| 82 | + 'users', # NEW ADD USERS APP IN THE INSTALLED APPS |
| 83 | + ] |
| 84 | + |
| 85 | + # NEW ADD ALL OF THIS |
| 86 | + REST_FRAMEWORK = { |
| 87 | + 'DEFAULT_AUTHENTICATION_CLASSES': [ |
| 88 | + 'users.authentication.backends.JWTAuthentication', |
| 89 | + ], |
| 90 | + 'DEFAULT_PERMISSION_CLASSES': [ |
| 91 | + 'rest_framework.permissions.IsAuthenticated', |
| 92 | + ], |
| 93 | + } |
| 94 | + ``` |
| 95 | + |
| 96 | +7. Let's create a file in the following directory in the **users** app [/users/authentication/backends.py](./users/authentication/backends.py). If you don't have the [authentication/](./users/authentication/) you MUST create it and also in the authentication folder add `__init__.py` file to make the module accessible. |
| 97 | + |
| 98 | +8. The following code implements the [JWT middleware](./users/authentication/backends.py) |
| 99 | + |
| 100 | +9. [Let's create our models in the user application](./users/models.py) |
| 101 | + |
| 102 | +10. [Let's create our managers in the user application](./users/managers.py) |
| 103 | + |
| 104 | +11. [Let's Implement the serializers](./users/serializers.py) |
| 105 | + |
| 106 | +12. [Let's Implement the Views](./users/views.py) |
| 107 | + |
| 108 | +13. [Let's Implement the user app URLS](./users/urls.py) and [core URL](./core/urls.py) |
| 109 | + |
| 110 | +14. After that you need to run migrations to create the migration files run: `python manage.py makemigrations` |
| 111 | + |
| 112 | +15. To apply the migrations and create the tables in the database run: `python manage.py migrate` |
| 113 | + |
| 114 | +16. If you want to test the application you will find the tests at [users/tests.py](./users/tests.py) run tests: `python manage.py tests` |
| 115 | + |
| 116 | +17. run the application `manage.py runserver` |
0 commit comments