Laravel 6 Login with Github Account Example

Laravel 6

Nicesnippets

3799

20-11-2019


hii guys,

In this example,I will show you how to login with github account in laravel 6 application.we will use laravel socialite package for login with github account in laravel 6 application.

Here we will learn how to add a github social login in your laravel 6 application and how to simple authenticate users using the Github Login.

Follow below step to use social authentication in your application with github:

Install Laravel 6

you will create laravel 6 fresh application.

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

Install Socialite

In this step we will install Socialite Package that provide fb api to connect with github account. So, first open your terminal and run bellow command.

composer require laravel/socialite

config/app.php

After install above package we should add providers and aliases in config file.

'providers' => [

....

Laravel\Socialite\SocialiteServiceProvider::class,

],

'aliases' => [

....

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

],

Install Auth

First you need to install laravel/ui package as like bellow:

composer require laravel/ui

You can use following commands for creating auth:

php artisan ui bootstrap --auth

Now you need to run npm command, otherwise you can not see better layout of login and register page.

Install NPM:

npm install

Run NPM:

npm run dev

Create Github App

In this step we need github client id and secret that way we can get information of other user. so if you don't have github app account then you can create from here :

Now you have to click on application name and you will get client id and client secret like as bellow screen :

config/services.php

you will create the index.php file in the views directory.

return [

....

'github' => [

'client_id' => 'xxx',

'client_secret' => 'xxx',

'redirect' => 'http://localhost:8000/callback/github',

],

]

Add Database Column

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

php artisan make:migration add_provider_and_github_id_column

<php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

class AddProviderAndGithubIdColumn extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

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

$table->string('provider');

$table->string('github_id');

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

//

}

}

app/User.php

Update mode like this way:

<php

namespace App;

use Illuminate\Notifications\Notifiable;

use Illuminate\Contracts\Auth\MustVerifyEmail;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable

{

use Notifiable;

/**

* The attributes that are mass assignable.

*

* @var array

*/

protected $fillable = [

'name', 'email', 'password', 'provider', 'github_id'

];

/**

* The attributes that should be hidden for arrays.

*

* @var array

*/

protected $hidden = [

'password', 'remember_token',

];

/**

* The attributes that should be cast to native types.

*

* @var array

*/

protected $casts = [

'email_verified_at' => 'datetime',

];

}

Create Routes

After adding provider and github_id column first we have to add new route for github login. so let's add bellow route in routes.php file.

app/Http/routes.php

Route::get('/auth/redirect/{provider}', 'SocialController@redirect');

Route::get('/callback/{provider}', 'SocialController@callback');

Create SocialController

We need to create a controller name SocialController. Use the below command and create Controller :

php artisan make:controller SocialController

SocialController.php

Put bellow code on your SocialController.php file.

<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('github_id', $getInfo->id)->first();

if (!$user) {

$user = User::create([

'name' => $getInfo->name,

'email' => $getInfo->email,

'provider' => $provider,

'github_id' => $getInfo->id

]);

}

return $user;

}

}

Resources/Views/Auth/register.blade.php

<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>

Resources/Views/Auth/login.blade.php

<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>

It will help you....


Recommended Posts