Laravel Google OAuth Authentication using Socialite Package
Socialite is a Laravel package that provides an elegant way to authenticate with OAuth providers. In this article, we will explore how to use Socialite to authenticate Google OAuth in a Laravel application.
Syntax
return Socialite::driver('google')->redirect();
Example
Step 1: Install Socialite
composer require laravel/socialite
Step 2: Create Google OAuth Client ID and Secret
- Go to the Google Cloud Console
- Select your project or create a new one
- Enable the Google People API
- Create new credentials (OAuth client ID)
- Choose web application and fill in the details
- Add
http://localhost:8000/auth/google/callback
as an authorized redirect URI - Click create and copy the Client ID and Secret
Step 3: Add Google OAuth Configuration in .env
GOOGLE_CLIENT_ID=your-client-id
GOOGLE_CLIENT_SECRET=your-client-secret
GOOGLE_REDIRECT=http://localhost:8000/auth/google/callback
Step 4: Create Google OAuth Routes
Route::get('auth/google', [GoogleController::class, 'redirectToGoogle']);
Route::get('auth/google/callback', [GoogleController::class, 'handleGoogleCallback']);
Step 5: Create GoogleController and Implement Socialite
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Laravel\Socialite\Facades\Socialite;
class GoogleController extends Controller
{
public function redirectToGoogle()
{
return Socialite::driver('google')->redirect();
}
public function handleGoogleCallback()
{
$user = Socialite::driver('google')->user();
// Add login code here
return redirect('/home');
}
}
Output
When a user clicks on the Google OAuth login button, they will be redirected to the Google login page to enter their credentials. After successfully logging in, they will be redirected back to your application.
Explanation
Socialite provides an easy way to integrate with third-party OAuth providers, such as Google, Facebook, Twitter, etc. In the example above, we use the redirectToGoogle
method to redirect the user to the Google login page, and the handleGoogleCallback
method to handle the response when the user returns.
Use
Google OAuth authentication is commonly used in web applications to allow users to sign in using their Google credentials instead of creating a new account.
Important Points
- Socialite provides an easy way to authenticate with OAuth providers in Laravel
- Google OAuth can be easily configured by creating a Google OAuth client ID and adding the credentials to the
.env
file. - The Socialite package automatically handles redirecting the user to the OAuth provider and handling the response when the user returns.
- After a successful login, the user can be redirected to the desired page.
Summary
In this article, we explored how to use Socialite to authenticate Google OAuth in a Laravel application. By integrating Socialite, we can easily add Google OAuth authentication to our web application in just a few steps, allowing users to sign in with their Google credentials and providing a better user experience.