Quick Guide: Add Google Login to Laravel
This guide provides the minimal steps to get Google login working in a Laravel project using Socialite.
Step 1: Get Google API Credentials
Go to the Google Cloud Console.
Create a new project.
Go to APIs & Services > OAuth consent screen.
Choose External and fill in the required app name, support email, and developer contact.
Go to APIs & Services > Credentials.
Click + Create Credentials > OAuth client ID.
Set Application type to Web application.
Under Authorized redirect URIs, add your callback URL. For local testing:
Click Create. Copy the Client ID and Client Secret.
Step 2: Install and Configure Laravel Socialite
Install the package:
composer require laravel/socialite- Add credentials to
.envGOOGLE_CLIENT_ID=YOUR_CLIENT_ID GOOGLE_CLIENT_SECRET=YOUR_CLIENT_SECRET GOOGLE_CALLBACK_URL=http://127.0.0.1:8000/auth/google/callback - Add config to
config/services.php'google' => [ 'client_id' => env('GOOGLE_CLIENT_ID'), 'client_secret' => env('GOOGLE_CLIENT_SECRET'), 'redirect' => env('GOOGLE_CALLBACK_URL'), ],
Step 3: Update User Database
Create a migration:
php artisan make:migration add_google_id_to_users_table --table=users- Edit the new migration file (in
database/migrations/):public function up() { Schema::table('users', function (Blueprint $table) { $table->string('google_id')->nullable()->unique()->after('id'); $table->string('password')->nullable()->change(); // Make password nullable }); } public function down() { Schema::table('users', function (Blueprint $table) { $table->dropColumn('google_id'); $table->string('password')->nullable(false)->change(); }); } - Run the migration:
php artisan migrate - Update User Model (in
app/Models/User.php):protected $fillable = [ 'name', 'email', 'password', 'google_id', // Add this ];
Step 4: Define Routes
In addition, add two routes for the login process:
use App\Http\Controllers\GoogleLoginController; Route::get('/auth/google/redirect', [GoogleLoginController::class, 'redirectToGoogle'])->name('google.redirect'); Route::get('/auth/google/callback', [GoogleLoginController::class, 'handleGoogleCallback'])->name('google.callback');Step 5: Create the Controller
Create the controller:
php artisan make:controller GoogleLoginController- Add the logic (to
app/Http/Controllers/GoogleLoginController.php): <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Laravel\Socialite\Facades\Socialite; use App\Models\User; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Str; class GoogleLoginController extends Controller { // Redirect user to Google public function redirectToGoogle() { return Socialite::driver('google')->redirect(); } // Handle the callback from Google public function handleGoogleCallback() { try { $googleUser = Socialite::driver('google')->user(); // Find or create user $user = User::updateOrCreate( ['google_id' => $googleUser->id], [ 'name' => $googleUser->name, 'email' => $googleUser->email, 'password' => Hash::make(Str::random(24)) // Set a random password ] ); // Log the user in Auth::login($user); return redirect('/dashboard'); // Redirect to your desired page } catch (\Exception $e) { // Handle error return redirect('/login')->with('error', 'Login with Google failed!'); } } }
Step 6: Add the Login Button
In your login.blade.php file (or any other view), add the link:
<a href="{{ route('google.redirect') }}"> Login with Google </a>
Conclusion:
Adding Google login to your Laravel app is a fast and powerful way to improve user experience. By using the Socialite package, you replace complex registration forms with a secure, one-click login, which can increase sign-ups and make your users happier.