Overview
The Inmobiliaria API supports Google OAuth 2.0 authentication, allowing users to sign in with their Google account without creating a separate password. This provides a seamless authentication experience and leverages Google’s security infrastructure.Google OAuth Configuration
OAuth is configured insrc/auth/index.ts:
Prerequisites
Before implementing Google OAuth, you need to set up a Google Cloud project:Create Google Cloud Project
- Go to Google Cloud Console
- Create a new project or select an existing one
- Enable the Google+ API for your project
Configure OAuth Consent Screen
- Navigate to APIs & Services > OAuth consent screen
- Select External user type (or Internal for Google Workspace)
- Fill in the required information:
- App name: “Ramirez Inmuebles” (or your app name)
- User support email
- Developer contact email
- Add scopes:
email,profile,openid - Add test users if in development mode
Create OAuth Credentials
- Go to APIs & Services > Credentials
- Click Create Credentials > OAuth client ID
- Select Web application
- Add authorized redirect URIs:
- Save the Client ID and Client Secret
OAuth Flow
The Google OAuth flow consists of three main steps:Flow Diagram
Implementation
Initiate Google Sign-In
Redirect users to the Google OAuth endpoint:GET /api/auth/sign-in/google
URL to redirect to after successful authentication (defaults to
FRONTEND_URL)This endpoint automatically redirects to Google’s OAuth consent screen. It should be opened in the same browser window (not via AJAX).
OAuth Callback
After user authorization, Google redirects to:Session Creation
On successful authentication:- Better Auth exchanges the authorization code for user information
- Creates or updates the user in the database
- Creates a new session
- Sets the session cookie
- Redirects to the
callbackURL
User Data Stored
When a user signs in with Google, the following data is stored:Client Implementation
Handling OAuth Callback
On your frontend callback page, check for the session:Account Linking
If a user signs up with email/password and later signs in with Google using the same email address, Better Auth will:- Link the Google account to the existing user
- Add a new record in the
accountstable - Update the user’s profile picture if they don’t have one
The
emailVerified field is automatically set to true when linking a Google account.Security Considerations
Token Storage
Token Storage
OAuth tokens (access, refresh, ID tokens) are:
- Stored encrypted in the
accountstable - Never exposed to the frontend
- Used only by the backend for Google API calls
- Automatically refreshed when expired (if refresh token available)
State Parameter
State Parameter
Better Auth automatically:
- Generates a random state parameter for each OAuth request
- Validates the state on callback to prevent CSRF attacks
- Rejects requests with invalid or missing state
Redirect URI Validation
Redirect URI Validation
Google validates that redirect URIs:
- Match exactly what’s configured in Google Cloud Console
- Use HTTPS in production
- Don’t contain fragments or query parameters
Scope Limitation
Scope Limitation
The API only requests essential scopes:
email: User’s email addressprofile: Basic profile information (name, picture)openid: OpenID Connect authentication
Customizing OAuth Behavior
You can customize OAuth behavior by modifying the Better Auth configuration:Troubleshooting
Error: redirect_uri_mismatch
Error: redirect_uri_mismatch
Cause: The redirect URI doesn’t match what’s configured in Google Cloud Console.Solution:
- Check your callback URL in Google Cloud Console
- Ensure it matches exactly:
https://api.yourdomain.com/api/auth/callback/google - Don’t include trailing slashes or query parameters
- Wait a few minutes after updating (Google caches configurations)
Error: access_denied
Error: access_denied
Cause: User clicked “Cancel” or denied permissions.Solution: Handle this gracefully on your frontend:
Session not created after redirect
Session not created after redirect
Cause: Cookies not being set or sent.Solution:
- Ensure
credentials: 'include'in fetch requests - Check CORS configuration allows credentials
- Verify
SameSite=NoneandSecure=truefor cross-origin - Check browser console for cookie warnings
Error: invalid_client
Error: invalid_client
Cause: Invalid Client ID or Client Secret.Solution:
- Verify
GOOGLE_CLIENT_IDandGOOGLE_CLIENT_SECRETin.env - Ensure credentials are from the correct Google Cloud project
- Check for extra spaces or newlines in environment variables
- Regenerate credentials if compromised
Testing OAuth Locally
To test Google OAuth in development:Use test users
If your OAuth consent screen is in testing mode, add your Google account as a test user.
Next Steps
Session Management
Learn how sessions work after OAuth authentication
Email/Password Auth
Combine OAuth with traditional authentication
User Profile
Access and update OAuth user information
Protected Routes
Protect routes with authentication middleware