Multiverse-of-Projects / NewsAI

A dynamic NewsAI dashboard that uses NLP to analyze news articles, visualize sentiment trends, and extract insights through interactive data visualizations.
https://news-ai-dashboard.streamlit.app/
GNU General Public License v3.0
14 stars 29 forks source link

Implement Authentication (Sign In/Sign Up) using Cookie-based JWTs #47

Open hem210 opened 2 weeks ago

hem210 commented 2 weeks ago

Description
We need to implement a simple cookie-based authentication system for the platform, allowing users to sign up and sign in. We will manage the access token using cookies to maintain session security. This will be done without OAuth or other third-party authentication providers.

Requirements:

  1. JWT Token Management:

    • Use a single JWT access token stored securely in an HTTP-only cookie.
    • The token should contain user information (e.g., user ID) and have an expiration time (e.g., 2-3 days).
  2. Authentication Flow:

    • Sign Up:
      • Endpoint to create a new user and securely hash the password before storing it.
      • Validate input to ensure email format and password strength.
    • Sign In:
      • Endpoint to authenticate users by validating their credentials (email and password).
      • Upon successful login, generate an access token and store it in an HTTP-only cookie.
    • Logout:
      • Endpoint to clear the authentication cookie, effectively logging the user out.
  3. Database Changes:

    • Create a User table in the database to store user information.
    • Ensure unique constraints on the email field to prevent duplicate accounts.
    • Optionally, add an index on the email field for efficient lookups during sign-in.
  4. Cookie Security:

    • Use HTTP-only and Secure flags to prevent client-side access to cookies.
    • Set the SameSite attribute to Lax or Strict to limit cross-site request usage.
    • Set the expiration of the cookie to match the JWT access token expiration.
  5. Security Considerations:

    • Passwords must be hashed securely using a library like bcrypt.
    • Ensure the JWT token is signed and validated correctly using a secret key.
    • Implement token expiration and invalidation.
  6. Error Handling & Logging:

    • Provide appropriate error responses for invalid credentials, expired tokens, and unauthorized access.
    • Log key authentication events like failed login attempts and token issues.

Files to create/change:


Folder Structure to Follow:

src/
├── api/
│   ├── main.py
│   ├── routes/
│   │   └── auth.py
│   ├── models/
│   │   └── user.py
│   ├── schemas/
│   │   └── auth.py
│   ├── dependencies/
│   │   └── auth.py
│   └── core/
│       ├── config.py
│       └── security.py
├── migrations/
│   └── create_users_table.sql

Checklist:


Considerations:

progGabo commented 2 weeks ago

Hi, is this issue still available? I would like to work on it.