In this tutorial you will learn about the Laravel 7/6 Socialite Google Login Example and its application with practical example.
In this Login with google in Laravel 7/6 example tutorial I’ll show you how to integrate google login in laravel 7/6 using socialite package. Integrating google login in Laravel 7/6 using socialite package is much easier. Allowing users to login with their social media accounts makes registration/login process much easier, it also encourages more users to register for your application.
- Laravel 7/6 Socialite Google Login Example
- Step1: Install laravel App
- Step 2: Add Database Details
- Step 3: Install Socialite Package For Google Login
- Step 4: Create Google App
- Step 5: Add Google App Details
- Step 6: Generate Auth File By Artisan
- Step 7: Add Routes
- Step 8: Create Controller
- Step 9: Add Socialite Google Login Button In Blade Views
- Step 10: Run Development Server
Laravel 7/6 Socialite Google Login Example
In this step by step tutorial, you will learn to integrate google login with your laravel 7/6 application. Please follow the steps give below:
- Install laravel App
- Add Database Details
- Install Socialite Package For Google Login
- Create Google App
- Set Google App Details
- Add Routes
- Generate Auth Files By Artisan
- Create Controller
- Add Socialite Google Login Button In Blade Views
- Run Development Server
Step1: Install laravel App
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2: Add Database Details
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name here DB_USERNAME=here database username here DB_PASSWORD=here database password here |
Step 3: Install Socialite Package For Google Login
In this step we will Install Socialite Package via Composer using following command:
1 |
composer require laravel/socialite |
Now, Navigate to config directory and open app.php file. Then add aliese and provider app.php:
1 2 3 4 5 6 7 8 9 |
'providers' => [ // Other service providers… Laravel\Socialite\SocialiteServiceProvider::class, ], 'aliases' => [ // Other aliases… 'Socialite' => Laravel\Socialite\Facades\Socialite::class, ], |
Step 4: Create Google App
Step 1:-
Visit Google Developer Console. And create a new project
Step 2:-
Please fill up the required information.
Step 3:-
After creating new project you need to select the created projects on the top menu. Then click on OAuth consent screen and select the given option according to your requirement:
Step 4:-
Next, you need to provide your website URL, privacy policy URL, etc.
Step 5:-
Click on left side menu credentials then select OAuth client ID.
Step 6:-
After that, the below form will be apper. Please select Web application from application type options. Once you have select Web application option, then one form will appear on web page. Here you have to define Name and you have also define Authorized redirect URIs field and lastly click on Create button.
Step 7:-
Once you have click on the create button, then you can get your Client ID and your client secret key.
Step 5: Add Google App Details
Now, we need to add google app details with redirect url. So, go to the config directory and open service.php file. And add the google client id, secret and redirect url in service.php file:
1 2 3 4 5 |
'google' => [ 'client_id' => 'xxxx', 'client_secret' => 'xxx', 'redirect' => 'http://127.0.0.1:8000/callback/google', ], |
After that, open User.php model and add the following properties into User.php model file:
1 2 3 |
protected $fillable = [ 'name', 'email', 'password', 'provider', 'provider_id' ]; |
Now, go to app/database folder and open create_users_table.php. And add the following code into your create_users_table.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique()->nullable(); $table->string('provider'); $table->string('provider_id'); $table->timestamp('email_verified_at')->nullable(); $table->string('password')->nullable(); $table->rememberToken()->nullable(); $table->timestamps(); }); } |
Before you run PHP artisan migrate command, Navigate to go to app/providers folder and open AppServiceProvider.php file. And add the following code into your AppServiceProvider.php file:
1 2 3 4 5 6 7 8 9 |
... use Illuminate\Support\Facades\Schema; .... function boot() { Schema::defaultStringLength(191); } ... |
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan migrate |
Step 6: Generate Auth File By Artisan
Install Laravel UI
1 |
composer require laravel/ui |
Create Auth
1 |
php artisan ui bootstrap --auth |
NPM Install
1 |
npm install |
Step 7: Add Routes
Now, you need to add routes in the web.php file. So navigate to routes folder and open web.php file. Then add the following routes into web.php file:
1 2 |
Step 8: Create Controller
Now, lets create a controller named GoogleLoginController using command given below –
1 |
php artisan make:controller GoogleLoginController |
Then go to app/controllers folder and open GoogleLoginController.php. And put the following code into your GoogleLoginController.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Validator,Redirect,Response,File; use Socialite; use App\User; class GoogleLoginController extends Controller { public function redirect($provider) { return Socialite::driver($provider)->redirect(); } public function callback($provider) { $getInfo = Socialite::driver($provider)->user(); $user = $this->createUser($getInfo,$provider); auth()->login($user); return redirect()->to('/home'); } function createUser($getInfo,$provider){ $user = User::where('provider_id', $getInfo->id)->first(); if (!$user) { $user = User::create([ 'name' => $getInfo->name, 'email' => $getInfo->email, 'provider' => $provider, 'provider_id' => $getInfo->id ]); } return $user; } } |
Step 9: Add Socialite Google Login Button In Blade Views
Go to Resources/Views/Auth folder and open register.blade.php. Then add a laravel socialite google login button in register.blade.php file:
1 2 3 4 5 6 |
<hr> <div class="form-group row mb-0"> <div class="col-md-8 offset-md-4"> <a href="{{ url('/auth/redirect/google') }}" class="btn btn-primary"><i class="fa fa-google"></i> Google</a> </div> </div> |
Next, go to Resources/Views/Auth folder and open login.blade.php. Then add a laravel socialite google login button in login.blade.php file:
1 2 3 4 5 6 |
<hr> <div class="form-group row mb-0"> <div class="col-md-8 offset-md-4"> <a href="{{ url('/auth/redirect/google') }}" class="btn btn-primary"><i class="fa fa-google"></i> Google</a> </div> </div> |
Step 10: Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 2 3 4 5 |
http://127.0.0.1:8000/login Or direct hit in your browser http://localhost/blog/public/login |