Overview
Handles the OAuth 2.0 callback from Google after user authorization. Exchanges the authorization code for access and refresh tokens, retrieves the user’s Gmail profile, stores tokens in the database, and sets up Gmail push notifications.Endpoint
Authentication
Authentication is verified via thestate parameter, which contains the original Supabase JWT token passed from auth-start.
Request
Query Parameters
Authorization code provided by Google OAuth after user consent
Supabase JWT token passed through the OAuth flow for session continuity and CSRF protection
OAuth error code if the user denied access or an error occurred during authorization
Example Request
This endpoint is called automatically by Google’s OAuth flow:Response
This endpoint performs a 302 redirect back to the frontend application with status parameters.Success Redirect
Redirects to:{FRONTEND_URL}/settings?success=true&email={gmail_email}
Error Redirect
Redirects to:{FRONTEND_URL}/settings?error=oauth_failed&reason={error_reason}
Error Reasons
User denied OAuth consent
Authorization code missing from callback
State parameter missing (CSRF protection)
State token verification failed
OAuth client credentials not configured
Failed to exchange authorization code for tokens
Failed to retrieve Gmail profile
Could not retrieve Gmail email address
Failed to save tokens to database
Failed to update existing token
Unexpected internal server error
Database Operations
Tokens Storage
Stores OAuth tokens in theuser_oauth_tokens table:
Token Updates
If a token already exists for the sameuser_id and gmail_email, the function updates the existing record instead of creating a duplicate. This handles reconnection scenarios.
Gmail Watch Setup
After storing tokens, the function automatically configures Gmail push notifications:- Calls Gmail API’s
watchendpoint with the user’s access token - Subscribes to
INBOXlabel changes - Configures Google Cloud Pub/Sub topic for notifications
- Stores watch metadata in
gmail_watchestable
Implementation Details
Environment Variables Required
GOOGLE_CLIENT_ID- Google OAuth client IDGOOGLE_CLIENT_SECRET- Google OAuth client secretOAUTH_REDIRECT_URI- OAuth callback URL (this endpoint)FRONTEND_URL- Frontend application URL for redirectsSUPABASE_URL- Supabase project URLSUPABASE_ANON_KEY- Supabase anonymous key (for token verification)SUPABASE_SERVICE_ROLE_KEY- Supabase service role key (for database operations)GOOGLE_PROJECT_ID- Google Cloud project ID (for Gmail watch setup)PUBSUB_TOPIC- Pub/Sub topic name (default:gmail-notifications)
OAuth Token Exchange
The function exchanges the authorization code for tokens by making a POST request to:client_idclient_secretcode(authorization code)grant_type=authorization_coderedirect_uri
Gmail Profile Retrieval
Retrieves user’s Gmail profile from:emailAddress field used to identify the connected account.
Watch Configuration
Configures push notifications via Gmail API:Gmail watches expire after 7 days and must be renewed. See renew-watches for automatic renewal.
Related Functions
- auth-start - Initiates the OAuth flow
- gmail-webhook - Processes notifications from Gmail watches
- renew-watches - Renews expiring Gmail watches