This project is a full-stack authentication and user management system built with:
- Backend β Flask (REST API with Flask-RESTX, JWT authentication, email OTP verification)
- Frontend β Next.js (React + Context API for global state management)
- Database β SQLite : Stores users, hashed passwords, profile pictures, OTPs, and tokens
It provides a production-grade authentication flow with login, registration, secure JWT sessions, email verification, and profile management.
- β Secure JWT-based authentication (Access & Refresh tokens in HTTP-only cookies)
- β Email verification (OTP system) using Gmail SMTP
- β Profile picture upload and storage with database path reference
- β Session management with auto-refresh and logout handling
- β Role-based route protection (401 Unauthorized / 403 Forbidden separation)
- β
Interactive Swagger API Docs (
/docs) via Flask-RESTX - β Frontend middleware to guard protected routes
- β Global auth state management using React Context
- β Resposive Desing Tailwind CSS for attractive and responsive design
git clone https://github.com/Nisarsaeed/python-authentication-flow.git
cd python-authentication-flow- Python 3.12
- pip (Python package manager)
cd backend
python -m venv venv
venv\Scripts\activate
pip install -r requirements.txtCreate a .env file inside the backend/ folder with:
MAIL_USERNAME=youremailaddress
MAIL_PASSWORD=password
JWT_SECRET_KEY=your_random_secret_keyUse a Gmail App Password (16-digit code from Google β Security β App Passwords). Then add the generated credentials in the backend .env file
python app.pybackend will be live at http://127.0.0.1:5000/
- npm
cd frontend
npm installnpm run devfrontend will be live at http://localhost:3000/
User registers or logs in β Backend issues Access Token (30 min) + Refresh Token (7 days) stored in HTTP-only cookies.
-
Access token is used for API requests.
-
If access token expires β frontend automatically calls /auth/refresh-token.
-
If refresh token expired β user must log in again.
-
Logout clears tokens from cookies and frontend state.
-
User uploads image during registration/profile update.
-
File is stored in backend/uploads/.
-
Path saved in DB β returned with user details.
-
Frontend loads the images using the /user/image/filename
Run the seed_super_admin.py file to create predefined super admin through credentials defined in the file
cd backend
python seed_super_admin.py