This guide will help you set up Google OAuth for the Divemap application.
- A Google account
- Access to Google Cloud Console
- Go to Google Cloud Console
- Create a new project or select an existing one
- Enable the Google+ API and Google Identity Services
- In Google Cloud Console, go to "APIs & Services" > "OAuth consent screen"
- Choose "External" user type
- Fill in the required information:
- App name: "Divemap"
- User support email: Your email
- Developer contact information: Your email
- Add scopes:
openidemailprofile
- Add test users (your email addresses)
- Save and continue
- Go to "APIs & Services" > "Credentials"
- Click "Create Credentials" > "OAuth 2.0 Client IDs"
- Choose "Web application"
- Set the following:
- Name: "Divemap Web Client"
- Authorized JavaScript origins:
http://localhost(for development)https://your-domain.com(for production)
- Authorized redirect URIs:
http://localhost(for development)https://your-domain.com(for production)
- Click "Create"
- Copy the Client ID and Client Secret
- Copy
env.exampleto.envin the backend directory - Add your Google OAuth credentials:
GOOGLE_CLIENT_ID=your_google_client_id_here GOOGLE_CLIENT_SECRET=your_google_client_secret_here
- Copy
frontend/env.exampletofrontend/.env - Add your Google Client ID:
VITE_GOOGLE_CLIENT_ID=your_google_client_id_here
Run the database migration to add the google_id field:
-- Run this in your MySQL database
ALTER TABLE users ADD COLUMN google_id VARCHAR(255) UNIQUE;
CREATE INDEX idx_users_google_id ON users(google_id);cd backend
pip install -r requirements.txtcd frontend
npm install- Start the backend server
- Start the frontend development server
- Navigate to the login or register page
- You should see a "Sign in with Google" button
- Test the Google OAuth flow
-
"Invalid Client ID" error
- Verify the Client ID is correct in both frontend and backend
- Check that the domain is authorized in Google Cloud Console
-
"Redirect URI mismatch" error
- Ensure the redirect URI in Google Cloud Console matches your application URL
- For development, use
http://localhost
-
"Google Identity Services not loaded" error
- Check that the Google Client ID is set in the frontend environment
- Verify the Google Identity Services script is loading
-
Database errors
- Ensure the
google_idcolumn exists in the users table - Run the migration script if needed
- Ensure the
- Never commit your
.envfiles to version control - Use different Client IDs for development and production
- Regularly rotate your Client Secrets
- Monitor OAuth usage in Google Cloud Console
- Update the authorized origins and redirect URIs in Google Cloud Console
- Set the production environment variables
- Ensure HTTPS is enabled for production
- Update the frontend API URL to point to your production backend
If you encounter issues:
- Check the browser console for JavaScript errors
- Check the backend logs for authentication errors
- Verify all environment variables are set correctly
- Ensure the Google Cloud Console configuration is correct