- How do I install **google/auth** in a Laravel project?
- Run `composer require google/auth` to install the package. No additional Laravel-specific setup is required, though you’ll need to configure credentials via environment variables (e.g., `GOOGLE_APPLICATION_CREDENTIALS`) or Application Default Credentials (ADC).
- Does this package work with Laravel’s HTTP client (Guzzle adapter)?
- Yes. Use Guzzle middleware to inject the auth token into requests. For Laravel’s HTTP client, replace `Http::withOptions()` with a Guzzle handler stack that includes the `GoogleAuthMiddleware` from this library.
- What Laravel versions does **google/auth** support?
- The package itself is PHP 8.1+ compatible, but Laravel integration depends on Guzzle 6/7 (used in Laravel 8+). For Laravel 7, ensure you’re using Guzzle 6. It works with all modern Laravel versions (8.x, 9.x, 10.x).
- How do I cache OAuth tokens in Laravel?
- Use Laravel’s caching layer (e.g., `Cache::store('file')` or Redis) with the `FetchAuthTokenCache` class. Pass a PSR-6 cache instance (like `symfony/cache` or `predis`) to `ApplicationDefaultCredentials::getCredentials()` for persistent token storage.
- Can I use this for Google Cloud Run or serverless deployments?
- Absolutely. **google/auth** leverages Application Default Credentials (ADC), which automatically detects credentials in Cloud Run, GKE, or Workload Identity Federation. No manual credential handling is needed in these environments.
- What’s the best way to handle token refresh failures in Laravel?
- Wrap API calls in a retry loop with exponential backoff. Use Laravel’s `try-catch` blocks to log failed refreshes and notify admins via `Log::error()`. For critical failures, implement a fallback to manual credential re-entry.
- How do I secure service account credentials in Laravel?
- Store credentials in environment variables (e.g., `GOOGLE_APPLICATION_CREDENTIALS`) or a secrets manager like AWS Secrets Manager. Validate credentials before use by calling `ApplicationDefaultCredentials::validateCredentials()` to prevent injection attacks.
- Does this package support OAuth 2.0 user authentication (e.g., Google Sign-In)?
- No, this library focuses on **server-to-server** auth (service accounts, ADC). For user authentication (OAuth flows), use the **google/apiclient** package alongside this library or Laravel’s socialite providers like `socialiteproviders/google`.
- How do I integrate this with Laravel’s service container?
- Bind the credentials to Laravel’s container in `AppServiceProvider::register()`. Example: `$this->app->singleton(GoogleAuth::class, fn() => ApplicationDefaultCredentials::getCredentials([...scopes], cache: $this->app->make(CacheItemPoolInterface::class)));`.
- Are there alternatives to **google/auth** for Laravel?
- For Google API auth, this is the **official** library. Alternatives like `league/oauth2-google` are community-driven but lack ADC support or Google Cloud integration. For broader OAuth needs, consider `thephpleague/oauth2-client`, but it won’t handle Google’s unique credential formats.