``## Day 4
It's already four days into the #100daysofMiva coding challenge. ???
? Dive into Django: Building a Secure User Authentication API from Scratch!
Are you ready to take your Django skills to the next level? ? In this tutorial, I’ll be guiding you through creating a robust user authentication API using Django. Whether you're a seasoned developer or just starting, this step-by-step guide will walk you through setting up user registration, login, and token-based authentication.
By the end of this session, you'll have a solid understanding of how to:
Let’s get coding! ?✨
To do this you need to have python installed: Ensure Python is installed by running:
macOS/Linux:
Python is often pre-installed. You can check by running:
or
If needed, install Python via Homebrew (macOS) or package manager (Linux):
Windows:
macOS/Linux:
Create and activate a virtual environment:
Windows:
Create and activate a virtual environment:
Now what is a framework without its packages?...let's install the packages we will need.?
With the virtual environment activated, the commands to install Django and additional packages are the same across all operating systems:
Explanation:
`
macOS/Linux/Windows:
Create a Django project and app:
let us simplify the necessary things:
All OS:
Modify settings.py to include your app and installed packages.
File: auth_project/settings.py
Explanation shall we?:
INSTALLED_APPS: This is where you register your apps and third-party packages. Here, you add rest_framework for the API functionality, rest_framework_simplejwt for JWT authentication, and users (the app you created) for managing user-related tasks.
It's okay not to know all the steps at once...it just takes practice, you'll get it right
I hope you're following...it's not hard it's complex?
(I don't know if that worked?)...
moving on...?
Keep your eyes up from here on guys?
File: users/serializers.py
Explanation:
File: users/views.py
Explanation:
File: users/urls.py
This code is written in the app's URL
Explanation:
URL Patterns: These define the paths that map to the views.
**register/**: This URL will handle user registration.
Then go to your Project's File: **auth_project/urls.py**
and type this...?
Explanation:
Unto the next here you can rest well?...no pressure from here onward hehe..
Command:
The function/ purpose of doing this is that it applies changes to your database schema based on the models and fields you've defined in your project. the ones we've orchestrated above?
In other words, it keeps the project up to date
Command:
This command starts the Django development server, making your project accessible locally. (your local port)
Now let's see what we've done so far...
Using Postman
Open Postman (or any API testing tool you prefer).
Set up a new request
In the Body tab, select raw and JSON format.
Enter the following JSON data:
Body:
For this part, the Django-Rest Framework has a friendly user interface so it's easier to navigate here than others
If successful, you should receive a response with HTTP status code 201 Created and a JSON response containing the user data.
To ensure JWT authentication is working, test the token endpoint.
Using Postman:
4, Click Send.
You should receive a JSON response with access and refresh tokens:
**
**
Server Not Starting: Ensure you're in the correct directory and have activated your virtual environment.
Endpoint Errors: Double-check your URL paths and ensure your Django app is correctly set up with the URLs.
Invalid Responses: Verify that your API endpoints and serializers are correctly configured.
By following these steps, you should be able to successfully run your Django development server, test the registration endpoint, and verify token-based authentication.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3