How to login with GitHub account in Laravel 6 Application?

How to login with GitHub account in Laravel 6 Application?

In this Laravel tutorial, you will learn how to login with GitHub account in Laravel 6 application. We will use the Laravel socialite package

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 :

Laravel 6 Login with Github Account

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

Laravel 6 Login with Github Account

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}', '[email protected]');

Route::get('/callback/{provider}', '[email protected]');

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

Laravel php GitHub

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Php how to delete multiple rows through checkbox using ajax in laravel

In this article i will let you know to delete multiple rows through checkbox using ajax in laravel and before delete we will give a confirmation message.

5 Laravel’s Hidden Gems

Spread the love1. Stop on first validation error By default, Laravel will check for all validation rules and return a list of errors. But if you want to stop this process after first validation failure, that’s how you can achieve…Read More→

Some of the most frequent how tos in Laravel

Spread the loveHow to get relationship from relationship using With() in Laravel Some times there are cases where you want to get relationship from relationship in Laravel, that can be achieved via following: How to create multiple where clauses in…Read More→

10 Laravel Quick Tips

Spread the loveTip 1. Controllers Having Single Action In some situations you need a single action in a controller, if this is the case in Laravel you can achieve it by __invoke() method. Routes: Artisan command to generate this controller:…Read More→

How to Install Laravel PHP Framework with Nginx on Ubuntu 20.04

Laravel is the most popular, free, and open-source PHP framework in the world, known for its expressive and elegant syntax. Laravel is accessible, powerful, and offers some of the best web development tools required for large, robust, and modern applications. In this article, you will learn how to install the Laravel PHP Framework on Ubuntu 20.04 server running on the Nginx web server.