This repository hosts the codebase for the Information Security (CS3002) project on
This project implements a secure authentication system with FastAPI as the backend and Next.js as the frontend. Key features include user registration, OTP verification, secure login, and protected routes, all developed with security best practices in mind.
The directory tree is organized as follows:
secure-auth/
├── README.md # Project documentation
├── assets/
│ └── logo.png # Logo image for frontend
├── requirements.txt # Backend dependencies
├── secureauth-frontend/ # Next.js frontend application
│ ├── public/
│ │ ├── logo.png # Logo image for frontend display
│ │ └── brief.md # Markdown file for dashboard content
│ ├── src/
│ │ ├── components/ # Shared frontend components
│ │ ├── pages/ # Frontend pages
│ │ ├── styles/ # Global styles for frontend
│ │ └── utils/ # Utility functions (e.g., API requests)
│ ├── package.json # Frontend dependencies
│ └── .env.local # Frontend environment variables
├── src/ # FastAPI backend application
│ ├── app.py # Main FastAPI application
│ ├── config.py # Configuration and constants
│ ├── database.py # Database-related functions
│ ├── email_service.py # Functions for email services and OTPs
└── venv/ # Virtual environment for backend
Ensure the following are installed on your system:
- Python 3.8+ (for FastAPI backend)
- Node.js 14+ and npm (for Next.js frontend)
- Virtual Environment (recommended for Python)
cd secure-auth/srcFor Unix-based systems (Linux/macOS):
python3 -m venv ../venv
source ../venv/bin/activateFor Windows:
python -m venv ..\venv
.\venv\Scripts\activatepip install -r ../requirements.txtuse the command "python -m src.app" to run the code from root dir after installing reqs.
Create a .env file in the src directory and add the following environment variables:
# src/.env
SECRET_KEY=your_secure_secret_key
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
SUPABASE_URL=your_supabase_url
SUPABASE_KEY=your_supabase_key
OTP_EXPIRATION_TIME=5 # in minutesReplace the values of your_secure_secret_key, your_supabase_url, and your_supabase_key with actual secure keys.
uvicorn app:app --reloadThe FastAPI backend will now be running on http://127.0.0.1:8000.
In a separate terminal window:
cd secure-auth/secureauth-frontendnpm installCreate a .env.local file in the secureauth-frontend directory and add the following:
# secureauth-frontend/.env.local
NEXT_PUBLIC_API_BASE_URL=http://127.0.0.1:8000This points the frontend API requests to the local FastAPI backend. Adjust this value if you’re running the backend on a different host or port.
npm run devThe Next.js frontend will now be running on http://localhost:3000.
- Registration: Users register with a username, password, email, and name. Passwords are hashed before storage for security.
- OTP Verification: After registration, an OTP is sent via email for verification. OTPs are time-limited for added security.
- JWT Authentication: Upon successful login, a JWT token is generated to authenticate subsequent requests. Protected routes are accessible only with valid tokens.
- Environment Security: Secret keys and sensitive information are stored in environment variables, not in the source code.
- Responsive Design: Utilizes Tailwind CSS for clean and responsive design.
- Dashboard with Markdown Content: Displays project brief and security measures by rendering
brief.mdon the protected dashboard page. - Reusable Components: Components like the header (with logo) and layout wrapper ensure consistency across pages.
- Protected Routes: The dashboard is a protected route, accessible only to authenticated users with a valid JWT token.
- Password Hashing: Uses bcrypt hashing to securely store passwords.
- JWT Tokens: Signed with a secure secret key and have expiration times to limit risks associated with token theft.
- Environment Variables: Sensitive information is stored securely outside the codebase.
- CORS Configuration: Backend is configured to accept requests only from authorized origins.
- HTTPS: Ensure HTTPS is enabled in production to protect data in transit.
- Base URL:
http://127.0.0.1:8000 - Endpoints:
- POST
/signup: Register a new user - POST
/verify-otp: Verify OTP for user activation - POST
/login: Authenticate user and receive JWT token - GET
/protected-route: Access a protected route (requires valid JWT)
- POST
- Use a secure HTTPS connection for production environments.
- Replace environment variables with production secrets.
- Consider using Docker for containerized deployment.
This project is licensed under the MIT License.