Step by step guide to integrate sign with Gmail in Laravel.
In this blog I will be explaining how to integrate gmail sign-in in laravel which is completely free including the google api's. So let's get started.
1. Install the laravel project. (refer my previous blogs or visit the website mrunali.in)
2. Install Auth
3. Now we will see how to add credentials
Run the following command in your terminal to install laravel socialite package
composer require laravel/socialite
Now open the file app.php in config/app.php
'providers' => [
.................................................
Laravel\Socialite\SocialiteServiceProvider::class,
];
'aliases' => [
..................................................................
Socialite' => \Laravel\Socialite\Facades\Socialite::class,
];
Now open service.php in config/service.php
'google' => [
'client_id' => env('GMAIL_CLIENT_ID'),
'client_secret' => env('GMAIL_CLIENT_SECRET'),
'redirect' => env('GMAIL_URL'),
],
This will be used by the socialite driver so do not miss to add this.
Go to .env file** add the following lines of code
GMAIL_CLIENT_ID = xxxxxxxxxxx
GMAIL_CLIENT_SECRET = xxxxxxxxxx
GMAIL_URL = xxxxxxxxxx
4. Create 2 routes as follows
route::get('/social-login/{provider}',[App\Http\Controllers\SocialLoginController::class,'social_login']);
This route we will using in the html file to redirect the user to the OAuth provider.
route::get('/social-login/{provider}/callback',[App\Http\Controllers\SocialLoginController::class,'social_callback']);
This route is used for receiving the callback from the provider after authentication.
5.Create a separate controller using below command. type the command in terminal inside your project directory
php artisan make:controller SocialLoginController
After creating the controller open the controller in Http/Controllers/SocialLoginController.php and add the following code
public function social_login($provider){
return Socialite::driver($provider)->redirect();
}
public function social_callback($provider){
$userSocial = Socialite::driver($provider)->stateless()->user();
$users = User::where(['email' => $userSocial->getEmail()])->first();
if($users){
Auth::login($users);
Auth::login(Auth::user(), true);
return redirect('/home');
}else{
$user = User::create([
'name' => $userSocial->getName(),
'email' => $userSocial->getEmail(),
'provider_id' => $userSocial->getId(),
'provider' => $provider,
]); return redirect()->route('home');
}
}
You can create additional 2 columns provider_id and provider to track the user sign-in activity.
- Now the login.blade.php provided by the laravel or your custom login add a tag
<a <a href="{{url('social-login/google')}}" class="btn btn-block">Sign In Gmail</a>
I will explain about how to get the google credentials for this in another blog. And also by adding few lines of code to this we can see how to login using Github.
Did you find this article valuable?
Support Mrunali Khandekar by becoming a sponsor. Any amount is appreciated!