Your cart is currently empty!
How to implement github login in laravel
Hi guys,
Nowadays, user sessions are fewer on the website. You want to register a user on your platform. You should simplify the process of registration and login for your application. Laravel provides a socialite package to reduce the process, i.e., In the minimum steps, they connect the user to your application. In this article, we implement GitHub login in laravel.
Step-by-step guide on GitHub login in the laravel application
- Install the package in your application
Install the Socialite package on the laravel application. Run the following command.
composer require laravel/socialite
- Get Credentials and Configure it
For credentials, you need to GitHub account. Click on Setting >> Developer Setting. Click OAuth Apps >> Register a New Application >> Add Details of the application. Generate credentials for your app.
You can add credentials to the env file and add the environment variable on config/services.php against the GitHub variable.
GITHUB_CLIENT_ID=XXXXXXXXXXXXXXXXXXXX
GITHUB_CLIENT_SECRET=ccf4c78f249ddXXXXXXXXXXXX
GITHUNB_CALLBACK_URL=http://127.0.0.1:8000/auth/github/callback
You can add the below code in the services.php file.
'github'=>[
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect'=> env('GITHUNB_CALLBACK_URL')
]
- create a column for the user table
Make a migration file that will create a column for the user’s table. Run migration command in your application.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddGithubIdColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function ($table) {
$table->string('provider');
$table->string('provider_id')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropColumns('provider');
Schema::dropColumns('provider_id');
}
}
Run the php artisan migrate command. Add fields to fillable property on the User.php file.
protected $fillable = [
'name',
'email',
'password',
'provider',
'provider_id'
];
- Create a Controller for social login
Make a controller for social login and load the social drivers for social login. I am loading the GitHub driver.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Laravel\Socialite\Facades\Socialite;
use Exception;
use Inertia\Inertia;
use Illuminate\Support\Facades\Route;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class SocialController extends Controller
{
public function redirectToGithub()
{
return Socialite::driver('github')->redirect();
}
public function handleGithubCallback()
{
try {
$user = Socialite::driver('github')->user();
$finduser = User::where('provider_id', $user->id)->first();
if($finduser){
Auth::login($finduser);
return redirect('/');
}else{
$checkUser = User::where('email', $user->email)->first();
if($checkUser) {
$checkUser->provider = 'github';
$checkUser->provider_id = $user->id;
$checkUser->save();
Auth::login($checkUser);
} else {
$newUser = User::create([
'name' => $user->name,
'email' => $user->email,
'provider'=> 'github',
'provider_id'=> $user->id,
'password' => Hash::make('123456dummy')
]);
Auth::login($newUser);
}
return redirect('/');
}
} catch (Exception $e) {
return Inertia::render('Auth/Login', [
'canResetPassword' => Route::has('password.request'),
'status' => 'Something Went wrong!! Try later',
]);
}
}
}
- Define the route
You need to define a route for social login in the route/web.php file.
Route::get('auth/github', [SocialController::class, 'redirectToGithub']);
Route::get('auth/github/callback', [SocialController::class, 'handleGithubCallback']);
6. Updating the login view
Open resources/views/auth/login.blade.php. Add the below code and change the button style according to your application interface.
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<a href="{{url('/auth/github')}}" class="btn btn-secondary">Login with GitHub</a>
</div>
</div>
Now we are successfully integrating GitHub login to your application. Socialite presently supports authentication via Facebook, Twitter, LinkedIn, Google, GitHub, GitLab, and Bitbucket. Go to the application login page and click on the login with GitHub button. It redirected the user to the GitHub authorization page. The user authorizes the application, and it will redirect to the dashboard page.
Now your user will easily connect to your application. Do you want to check which mail tools are best for your application? Click Here. I hope that this post (How to implement GitHub login in laravel) has clarified how to integrate the Socialite package in laravel and load the drivers. If you have any questions, please leave a comment and I will respond as soon as possible.
So stay connected Or Subscribe to our newsletter for such articles. You will get notified when we publish a new article. Thank you for reading this article. Please share this article with your friend circle. That’s it for the day. Stay Connected!
Cheers