Skip to content

BE-10: User Login System Implementation #10

@tecnodeveloper

Description

@tecnodeveloper

Description:
Build login system using Flask/FastAPI + MongoDB. User enters email + password → backend checks → password verified → token created → user logged in → test with Postman → done.


User Story

Given user already has account
When user logs in
Then system should authenticate and return access token


Tasks


Backend Setup (Flask / FastAPI)

  1. Extend Auth Module

    • Use existing /app/routes/auth.py
    • Update controller + service for login

Login API

  1. Create Login Route

    • POST /auth/login
    • Accept JSON (email, password)
  2. Validate Input

    • Check required fields
    • Validate email format

User Verification

  1. Find User in MongoDB

    • Query users collection by email
    • If not found → return error
  2. Verify Password

    • Compare hashed password using bcrypt
    • If wrong → return error

JWT Authentication

  1. Install JWT Library

    • Install pyjwt or python-jose
  2. Generate Token

    • Create JWT token
    • Include user ID + email
    • Set expiry time
  3. Return Token

    • Send token in response
    • Include basic user info

Response Handling

  1. Success Response
  • Return "Login successful"
  • Return JWT token
  1. Error Handling
  • Invalid credentials
  • User not found
  • Proper status codes

**Postman Testing **

  1. Setup Postman
  • Open Postman
  • Create new POST request
  1. Test Login API
  • URL: http://localhost:8000/auth/login
  • Body → JSON:
{
  "email": "test@gmail.com",
  "password": "123456"
}
  1. Validate Response
  • Check token returned
  • Check status code (200)
  1. Test Edge Cases
  • Wrong password
  • Non-existing user
  • Empty fields

Frontend Setup

  1. Create Login Page
  • /login page
  • Form (email, password)
  1. Handle Form State
  • Capture inputs
  • Basic validation

Frontend ↔ Backend

  1. Connect Login API
  • Call /auth/login
  • Send credentials
  1. Store Token
  • Save JWT (localStorage / cookies)
  1. User Feedback
  • Show success message
  • Show error messages

Protected Routes

  1. Setup Auth Guard
  • Check token before accessing pages
  • Redirect if not logged in

Run & Validate

  1. Run Backend
  • Start server
  • Fix errors
  1. Test Full Flow
  • Login from Postman
  • Login from UI
  • Verify token stored

Acceptance Criteria

  • Login API works
  • Password verification correct
  • JWT token generated
  • Postman testing completed
  • Token stored on frontend
  • Protected routes working

Testing Steps

  1. Run backend
  2. Test login in Postman
  3. Verify token
  4. Test wrong credentials
  5. Login from frontend
  6. Access protected route

Definition of Done

  • Login system fully working
  • JWT authentication implemented
  • Postman tests verified
  • Frontend integration complete

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions