Google SSO Setup & API Guide¶
This document provides a comprehensive guide for setting up and using Google Single Sign-On (SSO) with the HomePot system.
1. Google Cloud Console Configuration¶
To enable Google Login, you must first register your application on the Google Cloud Console.
Step 1: Create a Project¶
- Go to the Google Cloud Console.
- Click Select a project > New Project.
- Give your project a name (e.g., "HomePot") and click Create.
Step 2: Configure OAuth Consent Screen¶
- In the sidebar, go to APIs & Services > OAuth consent screen.
- Select External and click Create.
- Fill in the required app information:
- App name: HomePot
- User support email: Your email
- Developer contact info: Your email
- Click Save and Continue.
- In the Scopes section, click Add or Remove Scopes and add:
.../auth/userinfo.email.../auth/userinfo.profileopenid- Finish the wizard.
Step 3: Create Credentials¶
- Go to APIs & Services > Credentials.
- Click Create Credentials > OAuth client ID.
- Select Web application as the application type.
- Add the following Authorized redirect URIs:
http://localhost:8000/api/v1/auth/callback(Backend callback)http://localhost:5173/auth/callback(If using frontend-initiated flow) Note: Ensure these match your environment variables exactly.- Click Create. You will receive your
Client IDandClient Secret.
2. Environment Setup¶
Add the following variables to your backend/.env file:
# Google OAuth Credentials
GOOGLE_CLIENT_ID=your_client_id_here.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=your_client_secret_here
GOOGLE_REDIRECT_URI=http://localhost:8000/api/v1/auth/callback
# Frontend Redirect Location
FRONTEND_URL=http://localhost:5173
# Security Settings
COOKIE_SECURE=false # Set to true in production (HTTPS)
COOKIE_SAMESITE=lax # "lax", "strict", or "none"
3. API Reference¶
1. Initiate Google Login¶
Endpoint: GET /api/v1/auth/login
Description: Generates the Google OAuth authorization URL.
Success Response (200 OK):
2. Google Callback Handling¶
Endpoint: GET /api/v1/auth/callback
Description: Receives the code from Google, exchanges it for tokens, creates/links the user in HomePot, sets an httpOnly cookie, and redirects to the dashboard.
Query Parameters: - code (string): The authorization code from Google.
Workflow: 1. Exchanges code for Google id_token. 2. Verifies the token and extracts email/name. 3. If the user doesn't exist, a new account is created automatically. 4. Generates an internal App JWT. 5. Sets the JWT as an httpOnly cookie (access_token). 6. Redirects the browser to ${FRONTEND_URL}/dashboard.
4. How to Execute (Testing)¶
Manual Browser Test¶
- Start the backend:
uvicorn homepot.app.main:app --reload - Open your browser to
http://localhost:8000/api/v1/auth/login. - Copy the
auth_urlfrom the JSON response. - Paste it into your browser address bar.
- Log in with your Google account.
- Upon success, Google will redirect you to:
http://localhost:8000/api/v1/auth/callback?code=... - The backend will process the code and redirect you to
http://localhost:5173/dashboard.
Automated Tests¶
Run the provided test suite to verify the integration:
5. Security Notes¶
- JWT Storage: The
access_tokenis stored in anhttpOnlycookie. This prevents client-side JavaScript from accessing the token, significantly reducing XSS risks. - CSRF Protection: The cookie uses
SameSite=laxby default. - Production: Always set
COOKIE_SECURE=truein production to ensure tokens are only transmitted over HTTPS.