Login using Google Account in Laravel

 Login using Google Account in Laravel

Configuration for Google Login

Firstly we will install “laravel/socialite” in our project.

Use the following command to install it.

composer require laravel/socialite

When the installation is complete, Paste the “clientID”, “clientSecret” and redirect URI in .env file as:

GOOGLE_CLIENT_ID=xxxxxxxxxxxx-runcntjqat1j56ghqlip78eejl4pdakm.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=xxxxxx-sl9vkVFFmovXgAOp2ZSektxxxxxx
GOOGLE_REDIRECT_URL=http://127.0.0.1:8000/google/callback

Now add google service in config/services.php.

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

Routes and Controller Methods

Add the following two routes in web.php

Route::get('/google/redirect', [App\Http\Controllers\GoogleLoginController::class, 'redirectToGoogle'])->name('google.redirect');
Route::get('/google/callback', [App\Http\Controllers\GoogleLoginController::class, 'handleGoogleCallback'])->name('google.callback');

Now create a controller “GoogleLoginController” and add the following content to it

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Laravel\Socialite\Facades\Socialite;
use Illuminate\Support\Facades\Auth;
use App\Providers\RouteServiceProvider;

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


public function handleGoogleCallback()
{
$googleUser = Socialite::driver('google')->stateless()->user();
$user = User::where('email', $googleUser->email)->first();
if(!$user)
{
$user = User::create(['name' => $googleUser->name, 'email' => $googleUser->email, 'password' => \Hash::make(rand(100000,999999))]);
}

Auth::login($user);

return redirect(RouteServiceProvider::HOME);
}
}

Create Google Login Button in the FrontEnd

Now add an anchor tag in the login blade file as:

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

That’s all we need to do.




Post a Comment

0 Comments

Visual Studio