Google Login with Laravel 12 Using Socialite (Step-by-Step Guide)

Google Login with Laravel 12 Using Socialite (Step-by-Step Guide)

Google Login allows users to sign in quickly without creating a password. In this tutorial, you’ll learn how to implement Google OAuth Login in Laravel 12 using Laravel Socialite with a clean, scalable, and production-ready database structure.

What You Will Build

By the end of this tutorial, you will have:

  • Google OAuth Login

  • Automatic user creation

  • Google profile data storage

  • Secure authentication flow

  • Production-ready structure

  • Extendable setup (Facebook, GitHub, etc.)

Requirements

Make sure you have the following installed:

  • PHP 8.2+

  • Laravel 12

  • MySQL / MariaDB

  • Composer

  • Google Account

Step 1: Create Google OAuth Credentials

1.1 Google Cloud Console Setup

Visit the Google Cloud Console:

👉 https://console.cloud.google.com/

  1. Create a New Project

  2. Navigate to APIs & Services → OAuth consent screen

  3. Select External

  4. Fill in required fields:

    • App name

    • User support email

    • Developer email

  5. Save and continue

1.2 Create OAuth Client ID

  1. Go to APIs & Services → Credentials

  2. Click Create Credentials → OAuth Client ID

  3. Application type: Web Application

  4. Add Authorized Redirect URI:

http://127.0.0.1:8000/auth/google/callback
  1. Save and copy:

    • Client ID

    • Client Secret

Step 2: Install Laravel Socialite

Run the following command:

composer require laravel/socialite

Laravel 12 supports package auto-discovery, so no manual configuration is required.

Step 3: Configure Google Service

Open the services configuration file:

📄 config/services.php

'google' => [ 'client_id' => env('GOOGLE_CLIENT_ID'), 'client_secret' => env('GOOGLE_CLIENT_SECRET'), 'redirect' => env('GOOGLE_REDIRECT_URI'), ],

Step 4: Add Environment Variables

Open your .env file and add:

GOOGLE_CLIENT_ID=your_google_client_id GOOGLE_CLIENT_SECRET=your_google_client_secret GOOGLE_REDIRECT_URI=http://127.0.0.1:8000/auth/google/callback

Step 5: Create Google-Ready Users Table

Update your users table migration:

📄 database/migrations/create_users_table.php

Schema::create('users', function (Blueprint $table) { $table->id(); // Basic Info $table->string('name')->nullable(); $table->string('user_id')->nullable(); $table->string('email')->unique(); // Social Login $table->string('google_id')->nullable()->unique(); $table->string('provider')->nullable(); // User Metadata $table->string('join_date')->nullable(); $table->string('last_login')->nullable(); $table->string('phone_number')->nullable(); $table->string('status')->nullable(); $table->string('role_name')->nullable(); $table->string('avatar')->nullable(); $table->string('position')->nullable(); $table->string('department')->nullable(); $table->string('line_manager')->nullable(); $table->string('seconde_line_manager')->nullable(); // Authentication $table->timestamp('email_verified_at')->nullable(); $table->string('password')->nullable(); $table->rememberToken(); $table->timestamps(); });

Run the migration:

php artisan migrate

Step 6: Create Google Authentication Controller

Generate the controller:

php artisan make:controller Auth/GoogleAuthController

📄 app/Http/Controllers/Auth/GoogleAuthController.php

<?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use App\Models\User; use Illuminate\Support\Facades\Auth; use Laravel\Socialite\Facades\Socialite; use Illuminate\Support\Str; class GoogleAuthController extends Controller { public function redirect() { return Socialite::driver('google')->redirect(); } public function callback() { try { $googleUser = Socialite::driver('google')->stateless()->user(); if (empty($googleUser->email)) { return redirect()->route('login') ->withErrors('No email returned from Google account.'); } $user = User::where('google_id', $googleUser->id) ->orWhere('email', $googleUser->email) ->first(); if (!$user) { $user = User::create([ 'name' => $googleUser->name, 'email' => $googleUser->email, 'google_id' => $googleUser->id, 'provider' => 'google', 'avatar' => $googleUser->avatar, 'password' => bcrypt(Str::random(24)), 'role_name' => 'User', 'status' => 'Active', 'join_date' => now(), ]); } else { if (!$user->google_id) { $user->update([ 'google_id' => $googleUser->id, 'provider' => 'google', ]); } } $user->update([ 'last_login' => now(), ]); Auth::login($user); return redirect()->route('home'); } catch (\Exception $e) { \Log::error('Google OAuth Error', [ 'message' => $e->getMessage(), ]); return redirect()->route('login') ->withErrors('Authentication failed. Please try again.'); } } }

Step 7: Define Routes

📄 routes/web.php

use App\Http\Controllers\Auth\GoogleAuthController; Route::get('/auth/google', [GoogleAuthController::class, 'redirect']) ->name('google.login'); Route::get('/auth/google/callback', [GoogleAuthController::class, 'callback']);

Step 8: Add Google Login Button

📄 resources/views/auth/login.blade.php

<a href="{{ route('google.login') }}" class="btn btn-danger w-100"> <i class="bi bi-google"></i> Login with Google </a>

Step 9: Protect Dashboard Route

Route::get('/dashboard', function () { return view('dashboard'); })->middleware('auth')->name('home');

Step 10: Run & Test the Application

php artisan serve

Test Flow:

  1. Open login page

  2. Click Login with Google

  3. Select Google account

  4. Redirected to dashboard 🎉

Security Best Practices

  • Use HTTPS in production

  • Verify Google email address

  • Limit OAuth scopes

  • Auto-assign user roles

  • Enable account linking

  • Log authentication failures

Final Result

  • Google OAuth fully integrated

  • Automatic user creation

  • Secure login flow

  • Clean and scalable database

  • Laravel 12 compatible

  • Production-ready

Download Full Source Code

👉 GitHub:
https://github.com/StarCodeKh/Login-with-Laravel-12-Using-Socialite

Souy Soeng

Souy Soeng

Hi there 👋, I’m Soeng Souy (StarCode Kh)
-------------------------------------------
🌱 I’m currently creating a sample Laravel and React Vue Livewire
👯 I’m looking to collaborate on open-source PHP & JavaScript projects
💬 Ask me about Laravel, MySQL, or Flutter
⚡ Fun fact: I love turning ☕️ into code!

Post a Comment

CAN FEEDBACK
close