Prerequisites
Before implementing Google Sign-In, you need to:- Create a project in the Google Cloud Console
- Configure the OAuth consent screen
- Create OAuth 2.0 credentials (Client ID and Client Secret)
- Configure authorized redirect URIs
Google Cloud Console Setup
-
Create a Project
- Go to Google Cloud Console
- Click “Select a project” → “New Project”
- Name your project and click “Create”
-
Configure OAuth Consent Screen
- Go to “APIs & Services” → “OAuth consent screen”
- Choose user type (External for most applications)
- Fill in the required fields:
- App name
- User support email
- Developer contact information
- Click “Save and Continue”
- Add scopes if needed (profile and email are included by default)
- Click “Save and Continue”
-
Create OAuth Credentials
- Go to “APIs & Services” → “Credentials”
- Click “Create Credentials” → “OAuth client ID”
- Select “Web application” as the application type
- Add a name for your OAuth client
-
Configure Redirect URIs
- Under “Authorized JavaScript origins”, add:
http://localhost:3000(for local development)https://yourdomain.com(for production)
- Under “Authorized redirect URIs”, add:
http://localhost:3000/api/_internal/auth/google/callback(for local development)https://yourdomain.com/api/_internal/auth/google/callback(for production)
- Click “Create”
- Under “Authorized JavaScript origins”, add:
-
Save Credentials
- Copy your Client ID and Client Secret
- Store them securely (use environment variables or Modelence Cloud config)
Server-Side Configuration
Google Sign-In is built into Modelence and requires no additional packages.Option 1: Modelence Cloud (Recommended)
The preferred way to configure Google authentication is through Modelence Cloud:- Go to https://cloud.modelence.com/
- Navigate to your project
- Go to Authentication → Providers → Google
- Enable Google authentication
- Enter your Client ID and Client Secret from Google Cloud Console
- Save the configuration
Option 2: Environment Variables
Alternatively, you can use environment variables. Create a.env file in your project root:
.env to your .gitignore file to keep credentials secure.
The Google authentication is automatically enabled when these configurations are set. No additional server code is needed.
Client-Side Implementation
To initiate Google Sign-In, redirect the user to the Google authentication endpoint:UI Integration
Add a Google Sign-In button to your UI:How Google Sign-In Works
- Initiation - User clicks “Sign in with Google” button
- Redirect - User is redirected to Google’s OAuth consent screen
- Authorization - User grants permission to your app
- Callback - Google redirects back to your app with an authorization code
- Token Exchange - Your server exchanges the code for user information
- User Creation/Login - If user doesn’t exist, a new account is created; otherwise, user is logged in
- Session Creation - A session is established and the user is authenticated
User Data Handling
When a user signs in with Google, Modelence automatically:- Creates a user account if one doesn’t exist
- Links the Google account to the user profile
- Retrieves profile information (name, email, profile picture)
- Marks the email as verified (since Google has verified it)
Combining with Email/Password Authentication
Users can have both Google and email/password authentication on the same account:Troubleshooting
Redirect URI Mismatch Error- Ensure the redirect URI in Google Cloud Console exactly matches your application URL
- Check for trailing slashes and http vs https
- Common format:
https://yourdomain.com/api/_internal/auth/google/callback
- Verify your Client ID and Client Secret are correct
- Check that environment variables are properly loaded
- Ensure credentials are from the correct Google Cloud project
- Make sure Google+ API is enabled in your project
- Verify your OAuth consent screen is configured
- Check that your domain is authorized