How to add Google and GitHub auth into a laravel application

Do you know How to add Google and GitHub auth into a laravel application ?

It’s very easy to implement Google and GitHub authentication into your Laravel application. we are using the Laravel socialite package to do that. I will show you how easy to make that happen.

I hope you are familiar with Laravel so I am not covering the basics.

First, we have to install Laravel socialite package in our application

composer require laravel/socialite

Now we have to configure it inside our config/services.php file.

Add these codes to your services.php file

'github' => [
    'client_id' => env('GITHUB_CLIENT_ID'),
    'client_secret' => env('GITHUB_CLIENT_SECRET'),
    'redirect' => '/auth/github/callback',
],
'google' => [
    'client_id' => env('GOOGLE_CLIENT_ID'),
    'client_secret' => env('GOOGLE_CLIENT_SECRET'),
    'redirect' => '/auth/google/callback',
],

 

Now we have to create a controller

php artisan make:controller socialauthcontroller

Open the controller and add these functions

// enter controller code

use Laravel\Socialite\Facades\Socialite;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;


class socialauthcontroller extends Controller
{
    public function githubredirect(Request $request)
    {
        return Socialite::driver('github')->redirect();
    }

    public function githubcallaback(Request $request)
    {
        $userdata = Socialite::driver('github')->user(); 

        //check login 
        $user = User::where('email', $userdata->email)->where('auth_type','github')->first();
        if($user)
        {
            Auth::login($user);
            return redirect('/dashboard');
        }
        else
        {
             //do register
            $uuid = Str::uuid()->toString();
            $user =new User();
            $user->name =$userdata->name;
            $user->email  =$userdata->email;
            $user->password =Hash::make($uuid.now());
            $user->auth_type ='github';
            $user->save();  
            Auth::login($user);
            return redirect('/dashboard');
        }

       
    }


    public function googleredirect(Request $request)
    {
        return Socialite::driver('google')->redirect();
    }
    public function googlecallaback(Request $request)
    {
        $userdata = Socialite::driver('google')->user(); 

        //check login 
        $user = User::where('email', $userdata->email)->where('auth_type','google')->first();
        if($user)
        {
            Auth::login($user);
            return redirect('/dashboard');
        }
        else
        {
             //do register
            $uuid = Str::uuid()->toString();
            $user =new User();
            $user->name =$userdata->name;
            $user->email  =$userdata->email;
            $user->password =Hash::make($uuid.now());
            $user->auth_type ='google';
            $user->save();  
            Auth::login($user);
            return redirect('/dashboard');
        }

       
    }
}

 

now edit the .env file and add credentials. if you don’t know how to get google and GitHub client id and secret please check the video i made a complete detailed explanation about this topic.

GITHUB_CLIENT_ID=xxxxxxxxxxxxxxxxxxxxxxx
GITHUB_CLIENT_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxx

GOOGLE_CLIENT_ID=xxxxxxxxxxxxxxxxxxxxxxxxx
GOOGLE_CLIENT_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx

 

Now what … we have to create the routes

use App\Http\Controllers\socialauthcontroller;
use Laravel\Socialite\Facades\Socialite;

Route::get('/auth/github/redirect', [socialauthcontroller::class, 'githubredirect'])->name('githubredirect');
Route::get('/auth/github/callback', [socialauthcontroller::class, 'githubcallaback'])->name('githubcallaback');

Route::get('/auth/google/redirect', [socialauthcontroller::class, 'googleredirect'])->name('googleredirect');
Route::get('/auth/google/callback', [socialauthcontroller::class, 'googlecallaback'])->name('googlecallaback');

 



Okay that’s done. now we have to test it. if that working and you are getting your profile information now we can move on to create a use with that .

Now go to  yourwebsite.com/auth/github/redirect  to check github login

and  go to  yourwebsite.com/auth/google/redirect  to check google login

Okay seems nice ..

You could able to log in with google and Github now.

 

Conclusion:

Its very easy to make social authentication into websites. and anybody can able to do within minutes. In this tutorial you learned to install the socialite package, add config data, add API keys into env file, create a controller and create users from API output etc. hope this helps to make your web application more powerful.

 

Explainer Video About adding Google and GitHub auth into a Laravel application

 

Checkout more Awesome Laravel Tutorials Here

 

 

 

 

 

Post Your Questions on our forum

Post a question on Forum

Ajith Jojo Joseph

Self taught, dedicated young entrepreneur with many licensed products under his sleeve. Passionate about technology, business and excellence in general.

Share with your friends:

Comments are closed.

How to get data from a model on all pages in Laravel – Simple Way

Do you know How to get data from a model on all pages in Laravel ? In this blog post, […]

August 5, 2022

How to create a Laravel Collection From JSON

Today i have to create a laravel collection from a JSON object. I search on the internet and I cant […]

August 3, 2022

Laravel Eloquent WhereNull() and whereNotNull() Example

Do you know how to use WhereNull() and whereNotNull() Laravel Eloquent. In this post, I will explain how to use […]

July 28, 2022

Laravel Array to JSON Response From Laravel Controller

Do you know how to make JSON responses from a Laravel array? As a developer someday we all have to […]

July 28, 2022

Digital Marketing Toolkit

Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits

Get Free Access Now