Overview
CompuTécnicos supports Google Sign-In (OAuth 2.0) allowing users to authenticate using their Google accounts. The integration automatically creates user accounts for new users and syncs profile information (name, email, photo) with existing accounts.How It Works
The Google OAuth flow in CompuTécnicos:- User clicks “Sign in with Google”
- Redirected to Google’s authentication page
- User authorizes the application
- Google redirects back with an authorization code
- Application exchanges code for access token
- Retrieves user profile information (email, name, photo)
- Creates new account or updates existing account
- Logs the user in with a persistent session (30 days)
Prerequisites
- A Google Cloud Platform (GCP) account
- Access to Google Cloud Console
- PHP Google API Client library (installed via Composer)
Configuration Parameters
Google OAuth 2.0 Client ID for your application.Format:
123456789-abcdefghijklmnop.apps.googleusercontent.comThis identifies your application to Google’s OAuth service.Google OAuth 2.0 Client Secret.
Setup Instructions
Create Google Cloud Project
- Go to Google Cloud Console
- Click “Select a project” → “New Project”
- Enter project name (e.g., “CompuTécnicos”)
- Click “Create”
- Wait for the project to be created and select it
Enable Google+ API
- In your project, go to “APIs & Services” → “Library”
- Search for “Google+ API” or “Google People API”
- Click on the API
- Click “Enable”
The Google+ API is required for retrieving user profile information (email, name, photo).
Configure OAuth Consent Screen
- Go to “APIs & Services” → “OAuth consent screen”
- Select “External” user type (for public access)
- Click “Create”
- Fill in the required fields:
- App name: CompuTécnicos
- User support email: Your support email
- Developer contact: Your email
- Click “Save and Continue”
- Add scopes:
userinfo.emailuserinfo.profile
- Click “Save and Continue”
- Add test users if in testing mode
- 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
-
Configure the following:
Name: CompuTécnicos Web Client
Authorized JavaScript origins:
http://localhost:8080(development)https://yourdomain.com(production)
http://localhost:8080/google-callback.php(development)https://yourdomain.com/google-callback.php(production)
- Click “Create”
- Copy the Client ID and Client Secret
Implementation Details
Google Client Configuration
The Google OAuth client is configured with:OAuth Scopes
The integration requests the following scopes:- email: Access to user’s email address
- profile: Access to user’s basic profile info (name, photo)
Authentication Flow
Login Initiation (google-login.php):
google-callback.php):
- Exchange authorization code for access token
- Retrieve user information from Google
- Check if user exists in database
- Create new user or update existing user
- Set session variables
- Create persistent “remember me” token (30 days)
- Redirect to home page
User Data Handling
When a user authenticates: For new users:- Creates account with Google name, email, and photo
- Generates random password (required by schema)
- Assigns default role
- Creates persistent session token
- Updates name and photo from Google profile
- Maintains existing role and permissions
- Creates persistent session token
Retrieved User Information
Security Considerations
Protect Client Credentials
- Store Client ID and Secret in environment variables
- Never commit credentials to version control
- Use different credentials for development and production
- Add
.envto.gitignore
Validate Redirect URIs
- Only whitelist legitimate redirect URIs in Google Console
- Use HTTPS in production (required by Google)
- Validate the redirect URI server-side
Verify Tokens
- Always validate tokens server-side
- Check token expiration
- Verify the token issuer is Google
Secure User Data
- Store only necessary user information
- The profile photo URL from Google is stored directly (external URL)
- Hash any generated passwords using strong algorithms
Handle Sessions Securely
- Use secure, HTTP-only cookies
- Implement CSRF protection
- Rotate session tokens periodically
- The persistent token is valid for 30 days
User Experience
Success Flow
When authentication succeeds:- User is redirected to the home page (
index.php) - A success message is displayed:
- New users: “¡Bienvenido, ! Tu cuenta fue creada con Google y has iniciado sesión.”
- Existing users: “¡Bienvenido, ! Has iniciado sesión con Google.”
- Session persists for 30 days (remember me token)
- User profile photo from Google is displayed
Profile Photo Storage
The integration stores the Google profile photo URL directly in the database:Profile photos are hosted by Google, not uploaded to your server. This saves storage space but requires an active internet connection to display.
Troubleshooting
Error: “redirect_uri_mismatch”
Cause: The redirect URI in your code doesn’t match the ones configured in Google Console. Solution:- Check the redirect URI in your code:
base_url() . '/google-callback.php' - Ensure it exactly matches an authorized redirect URI in Google Console
- Include the protocol (
http://orhttps://) - Match the exact domain and path
- Save changes in Google Console and wait a few minutes
Error: “invalid_client”
Cause: Invalid Client ID or Client Secret. Solution:- Verify credentials in
.envmatch Google Console - Check for extra spaces or quotes
- Ensure you’re using the correct project in Google Console
- Regenerate credentials if necessary
Error: “access_denied”
Cause: User denied permission or OAuth consent screen not configured. Solution:- Verify OAuth consent screen is properly configured
- Check that required scopes are approved
- If in testing mode, ensure user is added as a test user
- Publish the OAuth consent screen for public access
Token Exchange Fails
Cause: Unable to exchange authorization code for access token. Solution:- Check server time is synchronized (OAuth tokens are time-sensitive)
- Verify SSL certificates are valid
- Ensure PHP has cURL extension enabled
- Check firewall allows outbound HTTPS to Google APIs
User Info Not Retrieved
Cause: API not enabled or insufficient scopes. Solution:- Enable Google+ API or People API in Google Console
- Verify
emailandprofilescopes are requested - Check API quotas haven’t been exceeded
- Ensure access token is valid
Profile Photo Not Displaying
Cause: Photo URL is invalid or external image loading is blocked. Solution:- Verify the photo URL is stored correctly in the database
- Check browser console for CORS or mixed content errors
- Ensure CSP headers allow images from
googleusercontent.com - Test the photo URL directly in a browser
Testing
Development Environment
- Use
http://localhost:8080as the authorized origin - Set redirect URI to
http://localhost:8080/google-callback.php - Test with your personal Google account
- Verify account creation and login
- Check profile information is synced correctly