In this tutorial you will learn about the Laravel 7/6 socialite Github Login Example and its application with practical example.
In this Login with Github in Laravel 7/6 example tutorial I’ll show you how to integrate Github login in laravel 7/6 using socialite package. Integrating Github 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 Github Login Example
- Step 1: Install Laravel App
- Step 2: Configure Database Detail
- Step 3: Create Github App
- Step 4: Add Github App Detail
- Step 5: Add Fillable Property in Model
- Step 6: Add fields Into User Migration File
- Step 7: Run Table Migration Command
- Step 8: Install Socialite Package
- Step 9: Create Auth Files By Artisan
- Step 10: Add Routes
- Step 11: Create Controller
- Step 12: Add Github Auth Button On Laravel View
- Step 13: Run Development Server
Laravel 7/6 socialite Github Login Example
In this step by step tutorial, you will learn to integrate Github login with your laravel 7/6 application. Please follow the steps give below:
- Install Laravel App
- Configure Database Detail
- Create Github App
- Add Github App Detail
- Install Socialite Package For Github Login
- Add Fillable Property in Model
- Add fields Into User Migration File
- Run Table Migration Command
- Create Auth Files By Artisan
- Add Routes
- Create Controller
- Add Github Auth Button On Laravel View
- Start Development Server
- Laravel 7, 6 Socialite GitHub Login App Looks Like
Step 1: 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: Configure Database Detail
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: Create Github App
In this step we will create github App for github social login. Use the link provided to create twitter app.
1 |
https://github.com/settings/developers |
Here you have to click New Auth APP button to Create your app. After this you need to register a new OAuth application page will open. So fill your app detail and submit it, Finally, you will see dashboard of your created github app. Now, copy your github app details.
Step 4: Add Github App Detail
In this step, add GitHub app details into service.php file. Go to config directory and open service.php file. Then add the client id and secret got from github app into service.php file:
1 2 3 4 5 |
'github' => [ 'client_id' => 'xxxx', 'client_secret' => 'xxx', 'redirect' => 'http://127.0.0.1:8000/callback/github', ], |
Step 5: Add Fillable Property in Model
Now, go to app folder and open User.php Model file update fillable property like following:
app/User.php
1 2 3 |
protected $fillable = [ 'name', 'email', 'password', 'provider', 'provider_id' ]; |
Step 6: Add fields Into User Migration File
In this step, go to database/migrations directory and open create_users_table.php file. Then add following code into create_users_table.php file to add some fields into users table
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 |
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ 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(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } } |
Step 7: Run Table Migration Command
Now, Navigate to app/providers directory and inside this directory find AppServiceProvider.php file and open it in any text editor. Then add the following code in 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 8: Install Socialite Package
In this step we will Install Socialite Package via Composer using following command:
1 |
composer require laravel/socialite |
After Installing ‘socialite’ package, we need to add service provider and alias in config/app.php file as following.
config/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 9: Create Auth Files By Artisan
Install Laravel UI
1 |
composer require laravel/ui |
Create Auth
1 |
php artisan ui bootstrap --auth |
NPM Install
1 |
npm install |
Step 10: Add Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 |
Route::get('/auth/redirect/{provider}', 'GithubSocialController@redirect'); Route::get('/callback/{provider}', 'GithubSocialController'); |
Step 11: Create Controller
Now, lets create a controller named GithubSocialController using command given below –
1 |
php artisan make:controller GithubSocialController |
After that, go to app/http/controllers directory and open GithubSocialController.php file. Then put the following code into GithubSocialController.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 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Validator,Redirect,Response,File; use Socialite; use App\User; class GithubSocialController 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 12: Add Github Auth Button On Laravel View
Now, go to resources/Views/Auth/ directory and open register.blade.php. After that, add the github login button into 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/github') }}" class="btn btn-primary"><i class="fa fa-github"></i> Github</a> </div> </div> |
So, navigate to resources/Views/Auth/ directory and open login.blade.php in any text editor. After that, add the github login button into 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/github') }}" class="btn btn-primary"><i class="fa fa-github"></i> Github</a> </div> </div> |
Step 13: 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 |