Laravel 8 Login with Github Example Tutorial

Admin   Laravel   290  2021-03-15 08:40:08

Hi Guys

Today, i will explain you step by step login with Github Account in laravel 8 using socialite. laravel 8 socialite provide api to login with github account. you can easily login using Github in laravel 8 application.

As we know social media is a become more and more popular in the world. every one have social account like github, facebook etc. i think also most of have github account. So if in your application have login with social then it become awesome. you got more people connect with your website because most of people does not want to fill sign up or sign in form. If there login with social than it become awesome.

So if you want to also implement login with github account then i will help you step by step instruction. let's follow tutorial and implement it.

Step 1 : Install Laravel 8

In the first step, we need to get fresh laravel 8 version application So let's open terminal and run bellow command to install fresh laravel project.

 

composer create-project --prefer-dist laravel/laravel blog

Step 2 : Database Configuration

In second step, we will make database Configuration for example database name, username, password etc. So lets open .env file and fill all deatils like as bellow:

.env

 

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=here your database name(blog)

DB_USERNAME=here database username(root)

DB_PASSWORD=here database password(root)

Step 3 : Install Jetstream

Here we need auth scaffolding of jetstream. i will give full example for how to install jetstream so click here.

Step 4 : Install Socialite

In this step, we need laravel/socialite package. you can install socialite package using bellow command so let's run bellow command :

 

composer require laravel/socialite

After install above package you can add providers and aliases in config file, Now open config/app.php file and add service provider and aliase.

config/app.php

 

....

'providers' => [

....

Laravel\Socialite\SocialiteServiceProvider::class,

],

'aliases' => [

....

'Socialite' => Laravel\Socialite\Facades\Socialite::class,

],

....

Step 5: Create Github Api

Here we need Github account that way we can get information of other user. so if you don't have Github account then you can create from here : Github. you can find bellow screen :

Now you have to click on Developer settings

Next you have to click on OAuth Apps

Here you have to click on Register a new application

Here you have to create on Register a new OAuth application

Now Redy Github APi

Now you have to set app id, secret and call back url in config file so open config/services.php and set id and secret this way:

config/services.php

 

return [

....

'github' => [

'client_id' => 'app id',

'client_secret' => 'add secret',

'redirect' => 'http://your-callback-url',

],

]

Step 6: Add Database Column

In this step first we have to create migration for add Github Id in your user table. So let's run bellow command:

 

php artisan make:migration add_github_id_column

 

<?php

use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class AddGithubIdColumn extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::table('users', function ($table) {

$table->string('github_id')->nullable();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::table('users', function ($table) {

$table->dropColumn('github_id');

});

}

}

Update mode like this way:

app/Models/User.php

 

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Foundation\Auth\User as Authenticatable;

use Illuminate\Notifications\Notifiable;

use Laravel\Fortify\TwoFactorAuthenticatable;

use Laravel\Jetstream\HasProfilePhoto;

use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable

{

use HasApiTokens;

use HasFactory;

use HasProfilePhoto;

use Notifiable;

use TwoFactorAuthenticatable;

/**

* The attributes that are mass assignable.

*

* @var array

*/

protected $fillable = [

'name',

'email',

'password',

'github_id'

];

/**

* The attributes that should be hidden for arrays.

*

* @var array

*/

protected $hidden = [

'password',

'remember_token',

'two_factor_recovery_codes',

'two_factor_secret',

];

/**

* The attributes that should be cast to native types.

*

* @var array

*/

protected $casts = [

'email_verified_at' => 'datetime',

];

/**

* The accessors to append to the model's array form.

*

* @var array

*/

protected $appends = [

'profile_photo_url',

];

}

Step 7: Create Routes

Here, we need to add resource route for login with gmail account. so open your "routes/web.php" file and add following route.

routes/web.php