cs421sp24 / project

1 stars 1 forks source link

User Authentication #5

Open madooei opened 4 months ago

madooei commented 4 months ago

Overview

User Story: As a user, I want to be able to register, log in, and log out of the application, so that I can access securely the application's features.

Description: Implement a secure user authentication system to allow users to register, log in, and log out of the application. The system should also support password encryption and session management. More concretely:

  1. Users should be able to register with an email and password.
  2. Users should be able to log in using their registered email and password.
  3. Users should be able to log out.
  4. Passwords should be encrypted before being stored in the database.
  5. The system should maintain user sessions for a seamless experience.

Design

Use Case for User Authentication

Sequence Diagram for User Authentication:

sequenceDiagram
    actor User
    participant Frontend
    participant AuthService
    participant UserService
    participant Database

    User->>Frontend: Enter email and password
    Frontend->>AuthService: validateUser(email, password)
    AuthService->>UserService: findUserByEmail(email)
    UserService->>Database: Query user by email
    Database-->>UserService: Return user
    UserService-->>AuthService: Return user
    AuthService->>AuthService: Compare passwords
    alt passwords match
        AuthService-->>Frontend: Return JWT token
        Frontend-->>User: Redirect to dashboard
    else passwords do not match
        AuthService-->>Frontend: Return error
        Frontend-->>User: Display error message
    end

In the sequence diagram, the flow starts with the user entering their email and password on the frontend. The frontend then calls the validateUser method in the AuthService, which in turn uses the UserService to find the user by email in the database. If the user is found, the AuthService compares the entered password with the stored password. If the passwords match, the user is authenticated, and a JWT token is returned to the frontend, which then redirects the user to the dashboard. If the passwords do not match, an error message is displayed to the user.

ERD for Database

erDiagram
    USER {
        integer id PK "Primary Key"
        string email "Unique Email"
        string password "Encrypted Password"
        datetime createdAt "Account Creation Timestamp"
        datetime updatedAt "Last Update Timestamp"
    }

UML for Backend

classDiagram
    class AuthService {
        +registerUser(userDto: CreateUserDto): Promise~User~
        +validateUser(email: string, password: string): Promise~string~
        +logoutUser(): Promise~string~
    }

    class UserService {
        +createUser(userDto: CreateUserDto): Promise~User~
        +findUserByEmail(email: string): Promise~User~
    }

    class User {
        -id: number
        -email: string
        -password: string
        -createdAt: Date
        -updatedAt: Date
    }

    AuthService --> UserService : uses
    UserService --> User : manages

API Documentation

Wireframes for Frontend

image

Tasks