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.
laravel/socialite) is built atop this library, ensuring native compatibility with Laravel’s authentication stack.clientId, clientSecret, redirectUri) and minimal boilerplate (e.g., session handling for state management).session()->put() for state storage).email, profile, openid) and custom scopes, enabling granular data access.hostedDomain) required?invalid_grant, access_denied) be surfaced to users?id_token) be performed client-side or delegated to Google’s API?Illuminate\Contracts\Auth\Authenticatable).laravel/socialite, this package can replace its Google provider (though Socialite adds Laravel-specific conveniences).league/oauth2-client (v2/v3).firebase/php-jwt (for id_token parsing) or league/oauth2-google’s built-in methods.laravel/framework for session/routing.composer require league/oauth2-google
GoogleAuthServiceProvider) to instantiate the provider:
$this->app->singleton(Google::class, function ($app) {
return new Google([
'clientId' => config('services.google.client_id'),
'clientSecret' => config('services.google.client_secret'),
'redirectUri' => $app['url']->route('google.callback'),
]);
});
GoogleAuthMiddleware)./auth/google → getAuthorizationUrl())./auth/google/callback → handle code/error).User model to hydrate from Google’s ResourceOwner:
$user = User::firstOrCreate([
'email' => $ownerDetails->getEmail(),
], [
'name' => $ownerDetails->getName(),
'google_id' => $ownerDetails->getId(),
]);
league/oauth2-google v4.x.id_token.https://www.googleapis.com/auth/drive).league/oauth2-google when new Google API requirements emerge.clientId/clientSecret) should be rotated periodically. Use Laravel’s environment variables (config/services.php) for easy updates.invalid_grant) for debugging.access_denied → "Login canceled").redirectUri in Google Console matches Laravel’s route.id_token) is CPU-light but may add latency. Cache validated tokens if needed.access_token for API calls (no server-side storage needed).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Google API downtime | Users unable to log in. | Implement fallback auth (e.g., email/password) or queue failed requests. |
Invalid state parameter |
CSRF attack or broken flow. | Enforce strict state validation (session-based). |
Expired access_token |
API calls fail. | Auto-refresh tokens using refresh_token. |
| Rev |
How can I help you explore Laravel packages today?