In this tutorial I’m going to learn how to log-in and sign-up with Gmail and Google accounts. Please follow some easy steps defined below. After following this tutorial you can log-in with Google.
Create Google App credentials
if you don’t have a Google app account then you can create one from here: Google Developers Console.
![](https://www.thedataops.org/wp-content/uploads/2023/12/consolegoogle1-1.png)
![](https://www.thedataops.org/wp-content/uploads/2023/12/consolegoogle2.png)
Step 2:-
![](https://www.thedataops.org/wp-content/uploads/2023/12/consolegoogle4-1024x630.png)
Step 3:- After filling above all steps are completed.
![](https://www.thedataops.org/wp-content/uploads/2023/12/Capture4-1024x501.png)
Step 4:-
![](https://www.thedataops.org/wp-content/uploads/2023/12/image-16.png)
Now OAuth client has been created successfully.
Now Go to your project:-
Let’s go to create authentication and run the below command
composer require laravel/socialite
![](https://www.thedataops.org/wp-content/uploads/2023/12/image-19.png)
Now update composer
Add provider in config/app.php file
![](https://www.thedataops.org/wp-content/uploads/2023/12/image-20.png)
Laravel\Socialite\SocialiteServiceProvider::class,
Also, add alias in the same config/app.php
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
![](https://www.thedataops.org/wp-content/uploads/2023/12/image-21.png)
Create a model
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
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use Socialite;
use App\SocialProvider;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
public function redirectToProvider()
{
return Socialite::driver('google')->redirect();
}
// add handleProviderCallback() function in RegisterController
public function handleProviderCallback()
{
try
{
$socialUser = Socialite::driver('google')->user();
}
catch(\Exception $e)
{
return redirect('/');
}
//check if we have logged provider
$socialProvider = SocialProvider::where('provider_id',$socialUser->getId())->first();
if(!$socialProvider)
{
//create a new user and provider
$user = User::firstOrCreate(
['email' => $socialUser->getEmail()],
['name' => $socialUser->getName()]
);
$user->socialProviders()->create(
['provider_id' => $socialUser->getId(), 'provider' => 'google']
);
}
else
$user = $socialProvider->user;
auth()->login($user);
return redirect('/home');
}
}
add routes in web.php
Route::get('auth/google', [App\Http\Controllers\Auth\RegisterController::class, 'redirectToProvider']);
Route::get('auth/google/callback', [App\Http\Controllers\Auth\RegisterController::class, 'handleProviderCallback']);
Let’s go to User.php and add this function
function socialProviders()
{
return $this->hasMany(SocialProvider::class);
}
![](https://www.thedataops.org/wp-content/uploads/2023/12/image-17.png)
Next go to SocialProvider.php and this function
![](https://www.thedataops.org/wp-content/uploads/2023/12/image-18.png)
protected $fillable = ['provider_id','provider'];
function user()
{
return $this->belongsTo(User::class);
}
.env
Add
client_id =Add your client_id
client_secret = Add your client_secret
redirect = https://www.wizbrand.com/auth/google/callback
![](https://www.thedataops.org/wp-content/uploads/2023/12/Capture6-1.png)
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>
![](https://www.thedataops.org/wp-content/uploads/2023/12/Capture5-1.png)
Now continue with Google
But after this, you can try in live because in callback function using HTTPS does not work in the local
![](https://www.thedataops.org/wp-content/uploads/2023/12/googlebug-1-1024x524.png)
so that’s why I said you try on live.
![](https://www.thedataops.org/wp-content/uploads/2023/12/image-23.png)
![](https://www.thedataops.org/wp-content/uploads/2023/12/after-login-google-1-1024x536.png)
I hope this tutorial is very helpful for you, if the code is not working properly please comment below thanks for reaching out.