I think almost all applications come with social login these days, and it makes sense to use google, facebook, github accounts to make our login process easier.
Laravel comes with several built-in features that make web development easier, including Socialite. Socialite is a Laravel package that provides an easy way to use social logins to authenticate users. We can use various OAuth providers like Google login, Facebook login, Twitter login, github login, and more.
In this tutorial, we will show you how to implement Google authentication using Laravel 9 Socialite. We will walk you through the process step by step and provide you with all the necessary code examples.
Before we start, make sure you have the following installed on your computer:
To use Google authentication with Laravel 9 Socialite, you first need to create a Google OAuth application. Follow the steps below to create your Google OAuth application:
To install Laravel 9 Socialite, run the following command in your terminal:
composer require laravel/socialite
After you have installed Laravel 9 Socialite, you need to add your Google OAuth credentials to your .env
file. Open your .env
file and add the following lines:
GOOGLE_CLIENT_ID=your_client_id GOOGLE_CLIENT_SECRET=your_client_secret GOOGLE_REDIRECT=http://localhost:8000/callback/google
Replace your_client_id
and your_client_secret
with the client ID and secret from your Google OAuth client.
Now that we have installed Laravel 9 Socialite and added our Google OAuth credentials to our .env
file, we need to create routes for our authentication flow. Open your routes/web.php
file and add the following code:
use Illuminate\Support\Facades\Route; use App\Http\Controllers\Auth\LoginController; Route::get('/login/google', [LoginController::class, 'redirectToGoogle'])->name('login.google'); Route::get('/callback/google', [LoginController::class, 'handleGoogleCallback']);
These routes will redirect the user to Google for authentication and handle the callback from Google.
Next, we need to create a LoginController
that will handle the authentication flow. Create a new file called LoginController.php
in the app/Http/Controllers/Auth
directory and add the following
<?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Auth; use Laravel\Socialite\Facades\Socialite; use App\Models\User; class LoginController extends Controller { public function redirectToGoogle() { return Socialite::driver('google')->redirect(); } public function handleGoogleCallback() { $user = Socialite::driver('google')->user(); $existingUser = User::where('email', $user->getEmail())->first(); if ($existingUser) { Auth::login($existingUser, true); } else { $newUser = new User; $newUser->name = $user->getName(); $newUser->email = $user->getEmail(); $newUser->google_id = $user->getId(); $newUser->save(); Auth::login($newUser, true); } return redirect('/dashboard'); } }
Finally, we need to create views for our authentication flow. Create a new file called login.blade.php
in the resources/views/auth
directory and add the following code:
@extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">{{ __('Login') }}</div> <div class="card-body"> <a href="{{ route('login.google') }}" class="btn btn-primary">{{ __('Login with Google') }}</a> </div> </div> </div> </div> </div> @endsection
In this tutorial, we have shown you how to implement Google authentication using Laravel 9 Socialite. We walked you through the process step by step and provided you with all the necessary code examples.
With this comprehensive guide, you will be able to implement Google authentication using Laravel 9 Socialite easily. We hope that this tutorial was helpful to you. Good luck!
Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…
Let's understand about how to use coding to improve your website's SEO. In today’s computerized…
Let's understand the most important linux commands for web developers. Linux, as an open-source and…
Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…
Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…
Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…
View Comments
Wonderful website. Lots of useful information here. I am sending it to a few pals ans also sharing in delicious. And of course, thanks to your effort!