Installation
composer require baks-dev/auth-google
php bin/console baks:assets:install
Configure Google OAuth Credentials
Add these to .env:
GOOGLE_CLIENT_ID=your_client_id.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=your_client_secret
Set Redirect URI
In Google Cloud Console, add https://{your-domain}/google/auth to Authorized Redirect URIs.
Run Migrations
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
First Use Case Trigger the auth flow via a route or button:
// In a controller or Blade template
return redirect()->route('google.auth.login');
Initiate Login Redirect users to Google’s OAuth endpoint:
use BaksDev\AuthGoogle\Auth\GoogleAuth;
$auth = new GoogleAuth();
$authUrl = $auth->getAuthorizationUrl();
return redirect($authUrl);
Handle Callback Process the OAuth response in a route:
Route::get('/google/auth/callback', [AuthController::class, 'handleCallback']);
public function handleCallback(GoogleAuth $auth)
{
$userData = $auth->handleCallback(request());
// Save user data to your DB (e.g., via a User model)
return redirect()->route('dashboard');
}
User Data Mapping Customize how Google data maps to your user model:
$auth->setUserMapper(function (array $googleData) {
return User::updateOrCreate(
['email' => $googleData['email']],
[
'name' => $googleData['name'],
'google_id' => $googleData['sub'],
]
);
});
<a href="{{ path('google.auth.login') }}" class="btn-google">Sign in with Google</a>
Route::middleware(['auth:google'])->group(function () {
// Protected routes
});
$auth->setScopes(['profile', 'email', 'openid']);
Redirect URI Mismatch
http vs https)..env:
APP_URL=https://your-domain.com
GOOGLE_REDIRECT_URI={{ APP_URL }}/google/auth
Missing Migrations
google_users table. Skipping migrations will cause handleCallback() to fail.php bin/console doctrine:migrations:migrate after installation.CORS Errors
Google Workspace Restrictions
Enable Logging
Add to config/packages/dev/monolog.yaml:
handlers:
google_auth:
type: stream
path: "%kernel.logs_dir%/google_auth.log"
level: debug
Then log auth events:
$auth->setLogger($this->container->get('logger'));
Test Locally Use Google’s OAuth Playground to validate scopes/secrets before deploying.
Custom User Model Override the default user mapping:
$auth->setUserModel(\App\Entity\CustomUser::class);
Post-Auth Actions Hook into the auth flow:
$auth->onAuthSuccess(function ($user) {
event(new GoogleAuthSuccess($user));
});
Token Refresh
Handle token expiration by extending the GoogleAuth class:
class CustomGoogleAuth extends GoogleAuth {
public function refreshToken($refreshToken) {
// Implement custom logic
}
}
Multi-Tenant Support Add tenant ID to the user mapper:
$auth->setUserMapper(function (array $googleData) use ($tenantId) {
return TenantUser::updateOrCreate(
['email' => $googleData['email'], 'tenant_id' => $tenantId],
['google_id' => $googleData['sub']]
);
});
GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET without spaces. Validate with:
php bin/console debug:container | grep google
baks:assets:install is required for templates/JS. Skip it only if you’re not using the default frontend assets.$token = $auth->getAccessToken();
Cache::put('google_token', $token, now()->addHours(1));
$user = session('google_user') ?? $auth->getUserData();
How can I help you explore Laravel packages today?