Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

laravelio/laravel.io

2497 stars
4 code files
View laravelio/laravel.io on GitHub

composer.json

Open in GitHub
{
//
"require": {
"php": "^8.0",
//
"laravel/socialite": "^5.0"
},
//
}

app/Http/Controllers/Auth/GithubController.php

Open in GitHub
use App\Http\Controllers\Controller;
use Laravel\Socialite\Facades\Socialite;
use Laravel\Socialite\Two\User as SocialiteUser;
 
class GithubController extends Controller
{
public function redirectToProvider()
{
return Socialite::driver('github')->redirect();
}
 
public function handleProviderCallback()
{
try {
$socialiteUser = $this->getSocialiteUser();
} catch (InvalidStateException $exception) {
$this->error('errors.github_invalid_state');
 
return redirect()->route('login');
}
 
try {
$user = User::findByGithubId($socialiteUser->getId());
} catch (ModelNotFoundException $exception) {
return $this->userNotFound(new GithubUser($socialiteUser->getRaw()));
}
 
return $this->userFound($user, $socialiteUser);
}
 
private function getSocialiteUser(): SocialiteUser
{
return Socialite::driver('github')->user();
}
//
}

routes/web.php

Open in GitHub
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\GithubController;
 
//
Route::namespace('Auth')->group(function () {
//
Route::get('login/github', [GithubController::class, 'redirectToProvider'])->name('login.github');
Route::get('auth/github', [GithubController::class, 'handleProviderCallback']);
});
//

resources/views/auth/login.blade.php

Open in GitHub
// ...
 
<a href="{{ route('login.github') }}" class="button button-dark mb-4">
<span class="flex items-center justify-center">
<x-icon-github class="inline h-4 w-4 mr-2" />
Github
</span>
</a>
 
// ...

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.