In this example i explain laravel 7 login with github Example. we will show laravel 7 socialite logitn with github. In this example you learn laravel 7 socialite provide api to login with github account. We can easily login using Github account in laravel project.
Larave 7 Login with Github account is a become more and more popular in the world. Every account is link by Github account and google account.
We will also discuss how to add a github social button in your laravel 7 application projects and how to simple authenticate users using the Github Login button in our laravel application. Also we learn GitHub register.
Read also : Laravel 7 Login with Linkedin using Socialite
Laravel 7 Login with Github Example
Step 1: Install Laravel 7
In this step i install fresh laravel 7 application and setup laravel 7 application. So run the following command to get laravel 7 application.
composer create-project --prefer-dist laravel/laravel blog
Setup Database
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 2: Install Laravel Socialite
In this step i install laravel socialite package that provide api to connect with github account. So run the following command on your terminal and get laravel socialite package.
Read also : Laravel 7 Socialite Login with Google Account Example
composer require laravel/socialite
After install the laravel socialite package we add providers and aliases in config file, So open your config/app.php file and add the following service provider and aliase.
'providers' => [ .... Laravel\Socialite\SocialiteServiceProvider::class, ], 'aliases' => [ .... 'Socialite' => Laravel\Socialite\Facades\Socialite::class, ],
Step 3: Create Github App
In this step we require Github client id and secret that way we can get other user information. so first you create Github app account then you can create from here : Github Developers Console.
After create account you can copy your client id and secret.
Now you have to set your app id, secret and call back url in config file so open the first config/services.php and set id and secret following code:
return [ .... 'github' => [ 'client_id' => 'xxxx', 'client_secret' => 'xxx', 'redirect' => 'http://127.0.0.1:8000/callback/github', ], ]
Step 4: Create Auth
In this step we install laravel ui and generate auth in laravel 7 application so, let’s run following command in your terminal:
Install Laravel UI (User Interface)
composer require laravel/ui
Create Auth:
php artisan ui bootstrap --auth
NPM Install:
npm install
Run NPM:
npm run dev
Step 4: Add Database Column
In this step i create migration for add provider_id in your user table. So first run bellow command:
php artisan make:migration add_provider_id_column
Migration : add_provider_id_column
<?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'); } }
Add provider_id column in your mode like this way:
app/User.php
protected $fillable = [ 'name', 'email', 'password', 'provider', 'provider_id' ];
Step 5: Create Routes
In this step we create route for login with github.
Route::get('/auth/redirect/{provider}', 'SocialController@redirect'); Route::get('/callback/{provider}', 'SocialController@callback');
Step 6: Create Controller
In this step we create controller and add method of github auth that method will handle github callback url and etc.
app/Http/Controllers/SocialController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Validator,Redirect,Response,File; use Socialite; use App\User; class SocialController 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; } }
In Resources/Views/Auth/register.blade.php and add a Github social login button :
<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>
In Resources/Views/Auth/login.blade.php and add a GitHub social login button :
<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 7: Start Development Server
php artisan serve
Now we are ready to run our laravel login with github using socialite packakge. so run the following command to quick run.
http://127.0.0.1:8000/login Or direct hit in your browser http://localhost/blog/public/login
I hope you understand of laravel 7 login with github example and it can help you..