This project demonstrates a Django REST API with JWT authentication (via django-rest-framework-simplejwt
), including two-factor authentication (2FA). The frontend is a simple Single Page Application (SPA) built with JavaScript (Fetch API).
is_verified = False
) and sends a 2FA code via email.is_verified = True
.sequenceDiagram
autonumber
participant User
participant Frontend (SPA)
participant Django (Backend)
participant DB
User->>Frontend (SPA): Opens signup form (Username/Email/Password)
Frontend (SPA)->>Django (Backend): POST /api/users/register/ with {username, email, password}
Django (Backend)->>DB: Stores new user (is_verified = False)
Django (Backend)->>User: Response (User ID)
Frontend (SPA)->>Django (Backend): POST /api/users/verify/send/ with { email }
Django (Backend)->>DB: Generates verification_code, stores it
Django (Backend)->>User: Sends email with verification_code
User->>Frontend (SPA): Clicks "Verify Code" and enters code
Frontend (SPA)->>Django (Backend): POST /api/users/verify/ with { email, code }
Django (Backend)->>DB: Sets is_verified = True, deletes code
Django (Backend)->>User: "User verified!"
style User fill:#f9f,stroke:#333,stroke-width:2px
style Frontend (SPA) fill:#bbf,stroke:#333,stroke-width:2px
style Django (Backend) fill:#bfb,stroke:#333,stroke-width:2px
style DB fill:#ff9,stroke:#333,stroke-width:2px
sequenceDiagram
autonumber
participant User
participant Frontend (SPA)
participant Django (Backend)
participant DB
User->>Frontend (SPA): Opens login form (Username/Password)
Frontend (SPA)->>Django (Backend): POST /api/users/login/ (username + password)
Django (Backend)->>DB: Verifies user data and is_verified
alt is_verified == True
Django (Backend)->>Frontend (SPA): {access: <token>, refresh: <token>}
Frontend (SPA)->>LocalStorage: Stores tokens
Frontend (SPA)->>User: "Login successful"
else is_verified == False
Django (Backend)->>Frontend (SPA): Error (HTTP 400/401)
Frontend (SPA)->>User: "Login failed. User not verified."
end
style User fill:#f9f,stroke:#333,stroke-width:2px
style Frontend (SPA) fill:#bbf,stroke:#333,stroke-width:2px
style Django (Backend) fill:#bfb,stroke:#333,stroke-width:2px
style DB fill:#ff9,stroke:#333,stroke-width:2px
pip install -r requirements.txt
python manage.py migrate
python manage.py runserver
The frontend is an SPA using pure JavaScript (Fetch API). To test it, you can:
index.html
locally or serve it using python -m http.server
.http://127.0.0.1:8000/api/users/...
) are correct.If you encounter cross-origin issues, enable CORS (e.g., via django-cors-headers
).
POST /api/users/register/
{ "username": "...", "email": "...", "password": "..." }
POST /api/users/verify/send/
{ "email": "..." }
verification_code
, stores it in the database, and sends it via email.POST /api/users/verify/
{ "email": "...", "code": "..." }
is_verified = True
.POST /api/users/login/
{ "username": "...", "password": "..." }
{ "access": "...", "refresh": "..." }
or error if is_verified = False
.GET /api/users/profile/
Authorization: Bearer <access-token>
PUT /api/users/profile/
{ "bio": "...", "avatar": "..." }
).Authorization: Bearer <access-token>
is_verified = True
.POST /api/users/token/refresh/
).localStorage.removeItem(...)
). Optionally, you can set up SimpleJWT blacklisting.