Skip to content

BE-12: Google OAuth Login Implementation #12

@tecnodeveloper

Description

@tecnodeveloper

Description:
Build Google login. User clicks Google → gets token → send to backend → verify → create/find user in MongoDB → generate JWT → user logged in → done.


User Story

Given user wants quick login
When user clicks Google login
Then system should authenticate and log user in


Tasks


Google Cloud Setup

  1. Create Google OAuth App

    • Go to Google Cloud Console
    • Create new project
    • Enable Google OAuth
  2. Get Credentials

    • Create OAuth Client ID
    • Add redirect URI (frontend URL)
    • Copy Client ID

Frontend Implementation

  1. Add Google Login Button

    • Add "Login with Google" button
    • Use Google OAuth library
  2. Integrate Google Auth

    • Install Google Identity Services
    • Initialize with Client ID
  3. Handle Login Response

    • Receive ID token from Google
    • Send token to backend

Backend Setup (Flask / FastAPI)

  1. Create Google Auth Route

    • POST /auth/google
    • Accept token from frontend
  2. Verify Google Token

    • Use Google token verification
    • Validate token authenticity
    • Extract user info (email, name)

MongoDB Integration

  1. Check Existing User

    • Search by email in users collection
  2. Create User if Not Exists

    • Save name, email
    • Add provider = "google"
    • Add created_at
  3. Avoid Duplicates

  • Ensure unique email

JWT Session Handling

  1. Generate JWT
  • Create token after login
  • Include user ID + email
  • Set expiry
  1. Return Response
  • Send JWT to frontend
  • Send user info

Postman Testing 🧪

  1. Setup Postman
  • Create POST request
  1. Test /auth/google
  • Send sample token (manual test)
  • Verify response
  1. Validate Errors
  • Invalid token
  • Expired token

Frontend Integration

  1. Handle Backend Response
  • Receive JWT
  • Store token (localStorage/cookies)
  1. User Redirect
  • Redirect to chat/dashboard after login
  1. UI Feedback
  • Show loading
  • Show error if login fails

Protected Routes

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

Run & Validate

  1. Run Full System
  • Start backend
  • Start frontend
  1. Test Full Flow
  • Click Google login
  • Complete login
  • Verify user stored in MongoDB
  • Verify JWT created

Acceptance Criteria

  • Google OAuth login works
  • Token verified in backend
  • User stored in MongoDB
  • JWT generated
  • Postman test completed
  • Frontend integration working

Testing Steps

  1. Run backend + frontend
  2. Click Google login
  3. Complete OAuth flow
  4. Check MongoDB user
  5. Verify JWT token
  6. Access protected routes

Definition of Done

  • Google OAuth fully implemented
  • MongoDB integration complete
  • JWT session working
  • Secure authentication flow

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