Integrating Google Authentication in Laravel with Socialite and Google Client PHP Library
Implementing Google authentication in your Laravel application can significantly enhance user experience by allowing users to log in using their existing Google accounts. This guide will walk you through integrating Google OAuth authentication using Laravel Socialite and extending its functionality with the Google Client PHP Library.
Why Integrate Google Authentication?
Integrating Google authentication offers several benefits:
- Convenience: Users can log in without creating a new account, reducing friction.
- Security: Leveraging Google’s robust authentication system enhances your application’s security.
- Access to Google Services: With user consent, your application can interact with Google services like Calendar and Drive.
Setting Up Laravel Socialite
Laravel Socialite simplifies OAuth authentication with various providers, including Google. Here’s how to set it up:
1. Install Laravel Socialite
Begin by installing Socialite via Composer:
composer require laravel/socialite
2. Configure Google Credentials
Add your Google OAuth credentials to the config/services.php
file:
'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect' => env('GOOGLE_REDIRECT_URI'),
],
Ensure these environment variables are set in your .env
file:
GOOGLE_CLIENT_ID=your-client-id
GOOGLE_CLIENT_SECRET=your-client-secret
GOOGLE_REDIRECT_URI=http://your-app-url.com/auth/callback
Creating Google OAuth Credentials
To obtain the necessary credentials:
- Visit the Google Cloud Console.
- Navigate to “APIs & Services” > “Credentials”.
- Set up your OAuth consent screen.
- Create new credentials and choose “OAuth client ID”.
- Select “Web application” and configure the redirect URI to match
GOOGLE_REDIRECT_URI
. - After creation, you’ll receive a Client ID and Client Secret. Download the credentials as a JSON file and place it at
storage/app/private/google/oauth-credentials.json
.
Implementing Authentication Routes
Define routes for redirecting users to Google’s OAuth page and handling the callback:
use Laravel\Socialite\Facades\Socialite;
use Illuminate\Support\Facades\Route;
Route::get('/auth/redirect', function () {
return Socialite::driver('google')
->scopes(['https://www.googleapis.com/auth/calendar'])
->with(['prompt' => 'consent'])
->redirect();
});
Route::get('/auth/callback', function () {
$googleUser = Socialite::driver('google')->user();
// Store tokens securely
Storage::disk('local')->put('google/oauth-token.json', $googleUser->token);
if ($googleUser->refreshToken) {
Storage::disk('local')->put('google/oauth-refresh-token.json', $googleUser->refreshToken);
}
// Authenticate user in your application
// ...
return redirect('/dashboard');
});
Refreshing Tokens
To handle token expiration and refresh:
Route::get('/auth/refresh', function () {
$refreshToken = Storage::disk('local')->get('google/oauth-refresh-token.json');
$newTokens = Socialite::driver('google')->refreshToken($refreshToken);
if ($newTokens->token) {
Storage::disk('local')->put('google/oauth-token.json', $newTokens->token);
}
if ($newTokens->refreshToken) {
Storage::disk('local')->put('google/oauth-refresh-token.json', $newTokens->refreshToken);
}
return redirect('/dashboard');
});
Integrating Google Client PHP Library
To interact with Google services like Calendar:
1. Install the Google Client Library
composer require google/apiclient
2. Set Up the Google Client
use Google\Client;
use Google\Service\Calendar;
$client = new Client();
$client->setAuthConfig(storage_path('app/private/google/oauth-credentials.json'));
$client->addScope(Calendar::CALENDAR_READONLY);
$client->setAccessToken(Storage::disk('local')->get('google/oauth-token.json'));
if ($client->isAccessTokenExpired()) {
$refreshToken = Storage::disk('local')->get('google/oauth-refresh-token.json');
$client->fetchAccessTokenWithRefreshToken($refreshToken);
Storage::disk('local')->put('google/oauth-token.json', $client->getAccessToken());
}
3. Access Google Services
$service = new Calendar($client);
$calendarList = $service->calendarList->listCalendarList();
foreach ($calendarList->getItems() as $calendar) {
echo $calendar->getSummary();
}
Conclusion
By integrating Laravel Socialite with the Google Client PHP Library, you can provide seamless Google authentication and access various Google services within your Laravel application. This approach enhances user experience and opens up possibilities for deeper integration with Google’s ecosystem.
Leave A Comment