Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication System Implementation and Frontend Integration #32

Merged
merged 8 commits into from
Nov 29, 2024

Conversation

AhmedFatthy1040
Copy link
Owner

Authentication System Implementation and Frontend Integration

Changes Made

Backend Authentication Implementation

  1. Created Authentication System Core:

    • authMiddleware.ts: JWT token verification middleware
    • authController.ts: Login and registration logic
    • User.ts: User model and type definitions
    • authRoutes.ts: Authentication route handlers
    • authService.ts: Core authentication business logic
    • Updated app.ts to include auth routes and middleware
  2. Added Authentication Features:

    • JWT-based authentication
    • User registration with password hashing
    • Secure login with credential verification
    • Protected route middleware
    • User model with TypeScript types
  3. Added CORS support:

    • Installed cors package and its types
    • Configured CORS middleware in app.ts
    • Set up proper CORS options for credentials

Frontend Implementation

  1. Created Authentication Service:

    • Added src/services/authService.ts for auth management
    • Implemented login, signup, and token management
    • Added TypeScript interfaces for auth requests/responses
  2. Updated Login Component:

    • Integrated with auth service
    • Added error handling and loading states
    • Implemented token storage
    • Improved UX with loading indicators
  3. Updated Signup Component:

    • Connected to auth service
    • Added password validation
    • Simplified form fields
    • Added error handling
  4. Enhanced Image Upload:

    • Added authentication token to requests
    • Improved error handling
    • Enhanced UX with loading states

Technical Details

Backend Authentication Flow

  1. User Registration:

    • Validate input data
    • Hash password with bcrypt
    • Store user in database
    • Generate JWT token
    • Return user data and token
  2. User Login:

    • Verify credentials
    • Compare password hash
    • Generate JWT token
    • Return user data and token
  3. Protected Routes:

    • Verify JWT token in middleware
    • Inject user data into request
    • Allow/deny access based on token validity

API Endpoints

  • POST /api/auth/register: User registration
  • POST /api/auth/login: User login
  • POST /api/images/upload: Protected image upload

Authentication Flow

  1. User signs up/logs in
  2. Backend validates and returns JWT
  3. Frontend stores token
  4. Protected routes use token
  5. Backend validates requests

Testing Instructions

Testing Authentication

  1. Register New User:
{
  "username": "testuser",
  "email": "[email protected]",
  "password": "password123"
}
  1. Login:
{
  "email": "[email protected]",
  "password": "password123"
}

Testing Protected Routes:

  • Login to get token
  • Use token for image upload
  • Try accessing without token (should fail)
  • Try with invalid token (should fail)

Security Implementation:

  • Secure password hashing with bcrypt
  • JWT token validation
  • Protected route middleware
  • CORS security
  • Input validation
  • Error handling
  • Secure token storage

Dependencies Added

Backend:

{
  "cors": "^2.8.5",
  "@types/cors": "^2.8.17",
  "bcrypt": "^5.1.1",
  "@types/bcrypt": "^5.0.2",
  "jsonwebtoken": "^9.0.2",
  "@types/jsonwebtoken": "^9.0.7"
}

Database Changes:

Created users table with:

  • id
  • username
  • email
  • password (hashed)
  • created_at

Related Issues

Future Improvements

  • Add password reset
  • Implement refresh tokens
  • Add session management
  • Enhance validation
  • Add rate limiting
  • Implement 2FA
  • Add OAuth providers

chore(types): add TypeScript types for new dependencies
feat(cors): add CORS configuration for frontend requests
feat(auth): implement login component with error handling
feat(auth): implement signup component with validation
feat(upload): add token authentication to image upload
refactor(components): update form handling and error states
feat(auth): implement authentication controller and service
Copy link
Collaborator

@justFadel19 justFadel19 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@AhmedFatthy1040 AhmedFatthy1040 merged commit 1b2a9ec into main Nov 29, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants