league/oauth2-google
Google OAuth 2.0 provider for thephpleague/oauth2-client. Implements OpenID Connect sign-in with Google, supports Authorization Code flow, and helps fetch user details and tokens using your Google client ID/secret. Compatible with PHP 8.x.
email, profile, openid) or refresh tokens for offline access.league/oauth2-client directly.*"This package lets us add Google SSO to our PHP/Laravel apps in <2 hours—no custom auth servers, no security risks from homegrown OAuth. It’s used by 400+ repos, supports compliance (OIDC), and unlocks:
- 30% faster user signups (Google’s 1.5B users).
- Lower support costs (fewer password resets).
- Seamless data sync (verified emails, profiles for CRM/personalization). Cost: Zero (MIT license). Risk: Minimal (backed by The PHP League, used in production by [list examples if available])."*
Ask: "Should we prioritize this for [QX] to reduce churn and improve onboarding?"
*"league/oauth2-google is a drop-in Google OAuth2/OIDC provider for Laravel/PHP, built on league/oauth2-client. Here’s why it’s the right choice:
- Standards-Compliant: Uses OpenID Connect (OIDC) for identity verification, aligning with modern auth best practices.
- Laravel-Friendly:
- Works with Laravel Passport, Sanctum, or custom sessions.
- Example integration:
$provider = new \League\OAuth2\Client\Provider\Google([ 'clientId' => env('GOOGLE_CLIENT_ID'), 'clientSecret' => env('GOOGLE_CLIENT_SECRET'), 'redirectUri' => route('google.callback'), 'scopes' => ['email', 'profile'], // Request only needed data ]);- Key Features:
- Scopes: Fetch
name,picture, or custom Google scopes.- Refresh Tokens: Enable
accessType: 'offline'for long-lived sessions (critical for APIs).- JWT Validation: Parse Google’s
id_tokenfor server-side verification (e.g., withfirebase/php-jwt).- G Suite Support: Restrict logins to
@yourdomain.comviahostedDomain.- Security:
- CSRF protection via
stateparameter (handled automatically).- No sensitive keys in client-side code (credentials stay server-side).
- Performance:
- ~50KB (small footprint).
- ~100ms latency for auth flow (benchmark: league/oauth2-client).
- Alternatives Compared:
Solution Pros Cons Fit for Us? Custom OAuth2 Full control High dev/maintenance cost ❌ No Google API PHP Client Workspace APIs Overkill for auth-only ❌ league/oauth2-google Lightweight, OIDC-compliant Google-only ✅ Best choice Recommendation:
- MVP: Integrate for user auth (scopes:
profile).- Phase 2: Add refresh tokens for background jobs (e.g., syncing Google Calendar).
- Audit: Use Google’s OAuth 2.0 Playground to test scopes.
Blockers to Address:
- Ensure
redirectUrimatches Google Cloud Console settings.- Store refresh tokens securely (e.g., Laravel’s
encryptionor a dedicated DB table).Next Steps:
- Create Google OAuth credentials in Cloud Console.
- Add
league/oauth2-googletocomposer.json.- Implement callback route (e.g.,
/auth/google/callback)."*
How can I help you explore Laravel packages today?