How to Implement “Login with Google” in Laravel 11

Prerequisites

Laravel project set up
Composer installed
Google Developer Account

Step 1: Set up Google OAuth Credentials

Go to the Google Developer Console
Create a new project or select an existing one
Enable the Google+ API


This content originally appeared on DEV Community and was authored by John Maths

Prerequisites

  • Laravel project set up
  • Composer installed
  • Google Developer Account

Step 1: Set up Google OAuth Credentials

  1. Go to the Google Developer Console
  2. Create a new project or select an existing one
  3. Enable the Google+ API
  4. Go to Credentials -> Create Credentials -> OAuth Client ID
  5. Select Web Application, add your app's URL and callback URL (e.g., http://your-app-url/login/google/callback)
  6. Note down your Client ID and Client Secret

Step 2: Install Laravel Socialite

Install Laravel Socialite via Composer:

composer require laravel/socialite

Step 3: Configure Socialite

Add the following to your config/services.php file:

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

Then, add these to your .env file:

GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret
GOOGLE_REDIRECT_URI=http://your-app-url/login/google/callback

Step 4: Set up Routes

Add these routes to your routes/web.php:

use App\Http\Controllers\Auth\GoogleController;

Route::get('login/google', [GoogleController::class, 'redirectToGoogle'])->name('login.google');
Route::get('login/google/callback', [GoogleController::class, 'handleGoogleCallback']);

Step 5: Create GoogleController

Create a new controller:

php artisan make:controller Auth/GoogleController

Implement the controller:

<?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;

class GoogleController extends Controller
{
    public function redirectToGoogle()
    {
        return Socialite::driver('google')->redirect();
    }

    public function handleGoogleCallback()
    {
        try {
            $user = Socialite::driver('google')->user();
            $finduser = User::where('google_id', $user->id)->first();

            if ($finduser) {
                Auth::login($finduser);
                return redirect()->intended('dashboard');
            } else {
                $newUser = User::create([
                    'name' => $user->name,
                    'email' => $user->email,
                    'google_id'=> $user->id,
                    'password' => encrypt('123456dummy')
                ]);

                Auth::login($newUser);
                return redirect()->intended('dashboard');
            }
        } catch (\Exception $e) {
            dd($e->getMessage());
        }
    }
}

Step 6: Update User Model

Add google_idto the fillable array in your User model:

protected $fillable = [
    'name',
    'email',
    'password',
    'google_id',
];

Step 7: Add Google ID to Users Table

Create a new migration:

php artisan make:migration add_google_id_to_users_table

In the new migration file:

public function up()
{
    Schema::table('users', function ($table) {
        $table->string('google_id')->nullable();
    });
}

public function down()
{
    Schema::table('users', function ($table) {
        $table->dropColumn('google_id');
    });
}

Run the migration:

php artisan migrate

Step 8: Add Login Button

In your login view, add a "Login with Google" button:

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


This content originally appeared on DEV Community and was authored by John Maths


Print Share Comment Cite Upload Translate Updates
APA

John Maths | Sciencx (2024-08-17T14:42:41+00:00) How to Implement “Login with Google” in Laravel 11. Retrieved from https://www.scien.cx/2024/08/17/how-to-implement-login-with-google-in-laravel-11/

MLA
" » How to Implement “Login with Google” in Laravel 11." John Maths | Sciencx - Saturday August 17, 2024, https://www.scien.cx/2024/08/17/how-to-implement-login-with-google-in-laravel-11/
HARVARD
John Maths | Sciencx Saturday August 17, 2024 » How to Implement “Login with Google” in Laravel 11., viewed ,<https://www.scien.cx/2024/08/17/how-to-implement-login-with-google-in-laravel-11/>
VANCOUVER
John Maths | Sciencx - » How to Implement “Login with Google” in Laravel 11. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/17/how-to-implement-login-with-google-in-laravel-11/
CHICAGO
" » How to Implement “Login with Google” in Laravel 11." John Maths | Sciencx - Accessed . https://www.scien.cx/2024/08/17/how-to-implement-login-with-google-in-laravel-11/
IEEE
" » How to Implement “Login with Google” in Laravel 11." John Maths | Sciencx [Online]. Available: https://www.scien.cx/2024/08/17/how-to-implement-login-with-google-in-laravel-11/. [Accessed: ]
rf:citation
» How to Implement “Login with Google” in Laravel 11 | John Maths | Sciencx | https://www.scien.cx/2024/08/17/how-to-implement-login-with-google-in-laravel-11/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.