# Google OAuth 2.0 Integration Plan ## Flow Architecture ```mermaid sequenceDiagram autonumber actor Customer participant Frontend as Storefront Next.js participant Google as Google Identity Services participant Backend as FastAPI CustomerAuthRouter participant DB as MySQL DB Customer->>Frontend: Clicks "Continue with Google" Frontend->>Google: Triggers OAuth 2.0 Authorization Code Flow Google-->>Customer: Prompts Google Sign-In & Consent Customer->>Google: Authenticates Google-->>Frontend: Redirects with Authorization Code Frontend->>Backend: POST /api/v1/customer/auth/google { code } Backend->>Google: Exchanges Code for Google ID Token & Profile Google-->>Backend: Returns Verified Email, Google ID, Name, Picture Backend->>DB: Lookup EcomCustomer by google_id or email alt Customer Exists Backend->>DB: Link google_id if missing, update last_login else New Customer Backend->>DB: Create new EcomCustomer record end Backend-->>Frontend: Returns JWT Access Token + Refresh Token Frontend->>Frontend: Merges Anonymous Guest Cart & Wishlist to Customer Account ``` --- ## Security Verification Rules 1. **Authorization Code Exchange**: Server-side exchange only (`/oauth2/v4/token`). Frontend never handles raw client secrets. 2. **Identity Verification**: Verify Google ID Token signature against Google's public keys (`https://www.googleapis.com/oauth2/v3/certs`). 3. **Account Linking**: If a customer registers via email `user@example.com` and later logs in via Google with `user@example.com`, safely link `google_id` to the existing account after verifying `email_verified == true`.