Skip to content

Latest commit

 

History

History
92 lines (70 loc) · 2.94 KB

File metadata and controls

92 lines (70 loc) · 2.94 KB

Google OAuth Implementation Summary

Backend Changes Made:

1. Google OAuth Configuration (config/passport.js)

  • Created Passport Google OAuth 2.0 strategy
  • Handles user creation and linking existing users
  • Manages user serialization/deserialization

2. User Model Updates (models/User.js)

  • Added googleId field for Google OAuth users
  • Made password optional for Google OAuth users
  • Updated password hashing logic to skip for Google users

3. Auth Routes (routes/auth.js)

  • Added /api/auth/google - Initiates Google OAuth flow
  • Added /api/auth/google/callback - Handles OAuth callback
  • Redirects to frontend with token and user data

4. Server Configuration (server.js)

  • Imported passport configuration
  • Added session middleware for passport
  • Added passport initialization

Frontend Changes Made:

1. Google Icon Component (components/ui/GoogleIcon.jsx)

  • Created reusable Google logo SVG component
  • Matches Google's official branding colors

2. Auth Store Updates (store/authStore.js)

  • Added googleAuth() method for initiating OAuth
  • Added handleGoogleCallback() for processing OAuth response

3. Auth Page Updates (pages/AuthPage.jsx)

  • Added Google Auth buttons to both login and signup forms
  • Added visual dividers ("Or continue with")
  • Improved error handling for URL parameters

4. Google Callback Page (pages/GoogleCallbackPage.jsx)

  • Handles OAuth redirect from backend
  • Processes token and user data
  • Provides loading state during authentication

5. App Router (App.jsx)

  • Added route for /auth/google/callback
  • Imported GoogleCallbackPage component

Environment Variables Required:

# Google OAuth Configuration
GOOGLE_CLIENT_ID=your_google_client_id_here
GOOGLE_CLIENT_SECRET=your_google_client_secret_here
GOOGLE_CALLBACK_URL=http://localhost:5000/api/auth/google/callback

# Frontend URL (for redirects after OAuth)
FRONTEND_URL=http://localhost:5173

# Session Secret (for passport sessions)
SESSION_SECRET=your_very_secure_session_secret_here

Setup Instructions:

  1. Get Google OAuth Credentials:

    • Go to Google Cloud Console
    • Create/select project
    • Enable Google+ API
    • Create OAuth 2.0 Client ID
    • Add redirect URI: http://localhost:5000/api/auth/google/callback
  2. Add Environment Variables:

    • Copy credentials to backend .env file
    • Set FRONTEND_URL and SESSION_SECRET
  3. Test the Flow:

    • Click "Sign in with Google" or "Sign up with Google"
    • Complete Google OAuth flow
    • Should redirect back and log user in

Features Implemented:

  • ✅ Google OAuth sign-in and sign-up
  • ✅ Automatic account linking for existing emails
  • ✅ Beautiful Google-branded buttons
  • ✅ Seamless user experience
  • ✅ Error handling for failed authentication
  • ✅ Responsive design
  • ✅ Animation and loading states

The implementation follows security best practices and provides a smooth user experience for both new and existing users.