A robust authentication API built with FastAPI, featuring user registration, login, logout, and token refresh functionality.
- Registration with username, email, and password
- Secure login with JWT authentication
- Logout with token blacklisting
- Token refresh for extended sessions
- Password hashing
- JWT token-based authentication
- Token blacklisting for security
- Rate limiting to prevent abuse
- Prisma ORM integration
- SQLite database (easily configurable to other databases)
- User and BlacklistedToken models
- Backend: FastAPI
- Database: SQLite (via Prisma ORM)
- Authentication: JWT (JSON Web Tokens)
- ORM: Prisma Client Python
- Clone the repository
git clone https://github.com/yourusername/fastapi-auth-system.git
cd fastapi-auth-system- Set up a virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies
pip install fastapi uvicorn prisma pydantic python-jose passlib bcrypt python-multipart-
Configure the database
- Create a
.envfile with your database URL:
DATABASE_URL="file:./BOT.sqlite"- Or update the
schema.prismafile directly with your database path
- Create a
-
Generate Prisma client
prisma generate- Run database migrations
prisma migrate dev --name init- Start the server
uvicorn main:app --reloadPOST /register- Register a new userPOST /login- Authenticate and receive tokensPOST /logout- Blacklist the current tokenPOST /refresh- Get a new access token using refresh tokenGET /me- Get current user information (protected)
The application uses two main models:
- id: Unique identifier (auto-incremented)
- username: Unique username
- email: Unique email address
- password: Hashed password
- id: Unique identifier (auto-incremented)
- token: Blacklisted JWT token
- expiresAt: Token expiration timestamp
- Passwords are hashed before storage
- JWT tokens have configurable expiration
- Refresh tokens provide extended sessions
- Token blacklisting prevents token reuse after logout
MIT License will be used
Pratyanj
Feel free to customize this README to better match your specific project implementation and requirements!