This repository boilerplate implementation of Django JWT authentication with a custom user model that includes email for authentication instead of username. It also includes fields for name, address, and phone number.
-
Clone the repository:
git clone https://github.com/DeepakRanaMagar/jwt-django-tutorial.git
-
Navigate to the project directory:
cd your-repo
-
Create a virtual environment:
python -m venv venv
-
Activate the virtual environment:
-
For Windows:
venv\Scripts\activate
-
For macOS and Linux:
source venv/bin/activate
-
-
Install the required dependencies:
pip install -r requirements.txt
-
Apply the database migrations:
python manage.py migrate
-
Start the development server:
python manage.py runserver
To use the Django JWT authentication with the custom user model, follow these steps:
-
Register a new user by making a
POST
request to/api/register
with the following parameters:email
: The user's email address.password
: The user's password.name
: The user's name.address
: The user's address.phone_num
: The user's phone number.
-
Log in by making a
POST
request to/api/login
with the following parameters:email
: The user's email address.password
: The user's password.
This will return a JSON response containing the JWT token.
-
Use the JWT token for subsequent authenticated requests by including it in the
Authorization
header:Authorization: Bearer <JWT_TOKEN>
Replace
<JWT_TOKEN>
with the actual JWT token obtained from the login response.
POST /api/register
: Register a new user.POST /api/login
: Log in and obtain a JWT token.
The custom user model used in this implementation includes the following fields:
email
: The user's email address (used for authentication).password
: The user's password.name
: The user's name.address
: The user's address.phone_num
: The user's phone number.
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for more information.