Skip to content

Implementation of session-based authentication in a FastAPI application. It provides a basic framework for user login, logout, and protected routes using server-side sessions.

Notifications You must be signed in to change notification settings

phiponatchi/fastapi-session-auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FastAPI Session based Authentication

This project demonstrates how to implement session-based authentication in a FastAPI application. It provides a basic framework for user login, logout, and protected routes using server-side sessions.

Features

  • User Login: Authenticates existing users and establishes a session.
  • Protected Routes: Restricts access to certain endpoints to authenticated users only.
  • Session Management: Uses server-side sessions to maintain user state.
  • Logout Functionality: Allows users to terminate their active session.

Getting Started

Prerequisites

  • Python 3.7+
  • uv (Python package installer)

Installation

  1. Clone the repository:

    git clone https://github.com/phiponatchi/fastapi-session-auth.git
    cd fastapi-session-auth
  2. Create a virtual environment and activate it:

    uv venv
  3. Install the dependencies:

    uv sync

Running the Application

To start the FastAPI application, run the following command:

uv run fastapi run main.py

The application will be accessible at http://127.0.0.1:8000.

API Endpoints

Authentication

  • GET /profile: Get Authenticated user profile (simple welcome message).
  • POST /login: Log in an existing user (test user: username=user, password=password).
  • POST /logout: Log out the current user.

Project Structure

├── main.py             # Main FastAPI application file
├── dto.py              # Pydantic models for request data
├── auth.py             # Authentication logic (hashing, session management)
├── db.py               # Database mockup and operations (e.g., python dict as db)
├── pyproject.toml.     # Project dependencies
└── README.md           # Project documentation

Contributing

Contributions are welcome! Please feel free to open issues or submit pull requests.

License

This project is licensed under the MIT License.

About

Implementation of session-based authentication in a FastAPI application. It provides a basic framework for user login, logout, and protected routes using server-side sessions.

Topics

Resources

Stars

Watchers

Forks

Languages