Login with Google in Laravel 10 App using Socialite Package | Add Sign-in with Google feature in Laravel

Posted by

I’m going to learn how to sign up and log in to a Google account using Gmail in this video. Please take the few simple actions listed below. You can log in using Google after following this tutorial.

Step 1: Install Laravel

composer create-project laravel/laravel laravel-google 

Step 2: Go to .env and set up connect with database

DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravelgoogle 
DB_USERNAME=root
DB_PASSWORD=

Step 3: Migrate table

php artisan migrate

Let’s go to create authentication and run below command

php artisan make:auth

Go to composer.json and paste this version

"laravel/socialite":"4.4.1",

Now update composer

composer update

add provider in config/app.php file

Laravel\Socialite\SocialiteServiceProvider::class,

add alias in config/app.php

'socialite' => Laravel\Socialite\Facades\Socialite::class,

Next create google App credentials here

if you don’t have google app account then you can create from here : Google Developers Console.

Now OAuth client created successfully.

Go to services.php and put your client id and client secret and callback url

'google' => [
        'client_id' => '346684678449-fbn3ngtcvddprrlmdsjco4kson26nvvi.apps.googleusercontent.com',
        'client_secret' => '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',
        'redirect' => 'https://www.wizbrand.com/auth/google/callback',
    
    ],
php artisan make:model SocialProvider -m

customize migration social_providers

$table->increments('id');
$table->integer('user_id')->unsigned()->references('id')->on('users');
$table->string('provider_id');
$table->string('provider');
$table->timestamps();
php artisan migrate

add socialProviders() function in user model

go to register controller and paste this code

add routes in web.php

Lets got to User.php and add this function below

function socialProviders()
    {
        return $this->hasMany(SocialProvider::class);
    }

Next go to SocialProvider.php and this function

protected $fillable = ['provider_id','provider'];
     function user()
    {
        return $this->belongsTo(User::class);
    }

Add Login with google Button On Blade File

<button class="btn btn-primary">
       <a class="text-white" href="{{ url('auth/google')}}">Google Login</a>
</button>

Now register page look like this and continue with google.

Now continue with google

Now Logged in Successfully…..

Thank you for reaching out. I hope this guide has been extremely useful. If the code is not working properly, please leave a comment below. 🙏🙏

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x