-
app/Http/Controllers/Auth/GithubController.php
Open in GitHubclass GithubController { public function __invoke(): Response { $githubUser = $this->socialite()->user(); $data = [ 'email' => $githubUser->getEmail(), 'name' => $githubUser->getNickname(), 'full_name' => $githubUser->getName(), 'github_access_token' => $githubUser->token, ]; $user = User::updateOrCreate(['id' => $githubUser->getId()], $data); abort_if($user->isBlocked(), Response::HTTP_FORBIDDEN); if (! $user->hasVerifiedEmail()) { $user->markEmailAsVerified(); } Bus::batch([ new UpdateUserDetails($user), new SyncUserOrganizations($user), new LoadUserRepositories($user), ])->onQueue('github')->dispatch(); Auth::login($user, false); $redirectTo = url(session()->pull('url.intended', route('home'))); return response(<<<HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="refresh" content="1;url=$redirectTo" /> <title>Redirecting to $redirectTo</title> </head> <body> Redirecting to <a href="$redirectTo">$redirectTo</a>. </body> </html> HTML); } public function redirect(): RedirectResponse { return $this->socialite() ->redirectUrl(route('auth.github.callback')) ->redirect(); } protected function socialite(): GithubProvider { return Socialite::driver('github'); } }
-
routes/auth.php
Open in GitHubuse App\Http\Controllers\Auth\GithubController; use Illuminate\Support\Facades\Route; Route::prefix('github')->name('github.')->group(static function (): void { Route::get('', [GithubController::class, 'redirect'])->name('redirect'); Route::get('callback', GithubController::class)->name('callback'); });