Google Login with Laravel Socialite Package

Google Login with Laravel Socialite Package

We have created “Google Login” for our test website http://stack-developers.xyz/

You can check at Login Page :- http://stack-developers.xyz/login-register

We will work on Google Login / Gmail Login functionality by which we can able to login into our website with our Gmail Id. We will install Laravel Socialite package for Google Login.

1) Install Laravel Socialite package :-
First of all, we will install Laravel Socialite package with below composer command :-
composer require laravel/socialite

If in case time out error comes then run below command :-
COMPOSER_MEMORY_LIMIT=-1 composer require laravel/socialite

2) Create your app in Google
Create a project: https://console.developers.google.com/projectcreate
Create credentials: https://console.developers.google.com/apis/credentials

Create OAuth consent screen
– Enter App Name
– Enter User Support Email
– Select User type as External
– Enter Website Home Page Link
– Enter Website Privacy Policy Link
– Enter Terms & Conditions Link

Generate Client Id and Client Secret

Below screenshots will help you to generate Client Id and Client Secret in Google console.

Screenshot 2021-06-09 at 9.28.37 PM

Screenshot 2021-06-09 at 9.28.10 PM

Screenshot 2021-06-09 at 9.22.31 PM

Screenshot 2021-06-09 at 9.21.54 PM

Screenshot 2021-06-09 at 9.21.41 PM

3) Update services.php file :-
Now update services.php file located at \config\ folder and add Client ID and Secret that we have generated along with redirect URL.

'google' => [
'client_id' => '437767528949-p639bq9g4jmo28s57ht0ihp3njsfsgg.apps.googleusercontent.com',
'client_secret' => 'whfwyWwTrJ9YenCBr4lXsfsf',
'redirect' => 'http://www.stack-developers.xyz/google/callback'
],

4) Create Routes :-
Now create below Routes in web.php file :-

// Google Login Routes
Route::get('/google/redirect', 'SocialAuthController@googleredirect');
Route::get('/google/callback', 'SocialAuthController@googlecallback');

5) Create Model Files :-
File :- SocialAccount.php
Command :- php artisan make:model SocialAccount

Content :-

Screenshot 2021-06-10 at 7.58.16 PM

————————–

File :- SocialAccountService.php
Command :- php artisan make:model SocialAccountService

Content :-

Screenshot 2021-06-10 at 7.57.02 PM

6) Create Controller File :-
File :- SocialAuthController.php
Command :- php artisan make:controller SocialAuthController

Content :-

Screenshot 2021-06-10 at 7.59.17 PM

7) Add Google Login Link :-
Now add Google Login link like below in your website :-

Screenshot 2021-06-10 at 8.04.26 PM