Skip to content

pratyanj/FastAPI-Login-and-Register

Repository files navigation

FastAPI Authentication System

A robust authentication API built with FastAPI, featuring user registration, login, logout, and token refresh functionality.

Features

User Management

  • Registration with username, email, and password
  • Secure login with JWT authentication
  • Logout with token blacklisting
  • Token refresh for extended sessions

Security

  • Password hashing
  • JWT token-based authentication
  • Token blacklisting for security
  • Rate limiting to prevent abuse

Database

  • Prisma ORM integration
  • SQLite database (easily configurable to other databases)
  • User and BlacklistedToken models

Tech Stack

  • Backend: FastAPI
  • Database: SQLite (via Prisma ORM)
  • Authentication: JWT (JSON Web Tokens)
  • ORM: Prisma Client Python

Setup

  1. Clone the repository
git clone https://github.com/yourusername/fastapi-auth-system.git
cd fastapi-auth-system
  1. Set up a virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies
pip install fastapi uvicorn prisma pydantic python-jose passlib bcrypt python-multipart
  1. Configure the database

    • Create a .env file with your database URL:
    DATABASE_URL="file:./BOT.sqlite"
    
    • Or update the schema.prisma file directly with your database path
  2. Generate Prisma client

prisma generate
  1. Run database migrations
prisma migrate dev --name init
  1. Start the server
uvicorn main:app --reload

API Endpoints

  • POST /register - Register a new user
  • POST /login - Authenticate and receive tokens
  • POST /logout - Blacklist the current token
  • POST /refresh - Get a new access token using refresh token
  • GET /me - Get current user information (protected)

Database Schema

The application uses two main models:

User

  • id: Unique identifier (auto-incremented)
  • username: Unique username
  • email: Unique email address
  • password: Hashed password

BlacklistedToken

  • id: Unique identifier (auto-incremented)
  • token: Blacklisted JWT token
  • expiresAt: Token expiration timestamp

Security Considerations

  • Passwords are hashed before storage
  • JWT tokens have configurable expiration
  • Refresh tokens provide extended sessions
  • Token blacklisting prevents token reuse after logout

License

MIT License will be used

Author

Pratyanj


Feel free to customize this README to better match your specific project implementation and requirements!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages