From ff8f7c745218540d899d461f20090e2a87a1ee6e Mon Sep 17 00:00:00 2001 From: orlowski11 Date: Tue, 11 Jun 2024 08:18:44 +0200 Subject: [PATCH] moved github auth functions to GithubController --- app/Http/Controllers/GithubController.php | 35 +++++++++++++++++++++++ app/Http/Controllers/UserController.php | 28 ------------------ config/services.php | 8 +++--- routes/web.php | 9 ++---- 4 files changed, 42 insertions(+), 38 deletions(-) create mode 100644 app/Http/Controllers/GithubController.php delete mode 100644 app/Http/Controllers/UserController.php diff --git a/app/Http/Controllers/GithubController.php b/app/Http/Controllers/GithubController.php new file mode 100644 index 00000000..22bd0d4b --- /dev/null +++ b/app/Http/Controllers/GithubController.php @@ -0,0 +1,35 @@ +redirect(); + } + + public function callback() + { + $githubUser = Socialite::driver("github")->user(); + + $user = User::updateOrCreate([ + "github_id" => $githubUser->id, + ], [ + "name" => $githubUser->nickname, + "email" => $githubUser->email, + "github_token" => $githubUser->token, + "github_refresh_token" => $githubUser->refreshToken, + ]); + + Auth::login($user); + + return redirect("/"); + } +} diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php deleted file mode 100644 index 56fd4c71..00000000 --- a/app/Http/Controllers/UserController.php +++ /dev/null @@ -1,28 +0,0 @@ -user(); - - $user = User::updateOrCreate([ - 'github_id' => $githubUser->id, - ], [ - 'name' => $githubUser->nickname, - 'email' => $githubUser->email, - 'github_token' => $githubUser->token, - 'github_refresh_token' => $githubUser->refreshToken, - ]); - - Auth::login($user); - - return redirect('/'); - } -} diff --git a/config/services.php b/config/services.php index afe181a0..4c9f2d64 100755 --- a/config/services.php +++ b/config/services.php @@ -17,9 +17,9 @@ "channel" => env("SLACK_BOT_USER_DEFAULT_CHANNEL"), ], ], - 'github' => [ - 'client_id' => env('GITHUB_CLIENT_ID'), - 'client_secret' => env('GITHUB_CLIENT_SECRET'), - 'redirect' => env('GITHUB_REDIRECT_URL'), + "github" => [ + "client_id" => env("GITHUB_CLIENT_ID"), + "client_secret" => env("GITHUB_CLIENT_SECRET"), + "redirect" => env("GITHUB_REDIRECT_URL"), ], ]; diff --git a/routes/web.php b/routes/web.php index 0fcb73cf..999740fa 100755 --- a/routes/web.php +++ b/routes/web.php @@ -2,14 +2,11 @@ declare(strict_types=1); +use App\Http\Controllers\GithubController; use Illuminate\Support\Facades\Route; use Inertia\Response; -use Laravel\Socialite\Facades\Socialite; -use App\Http\Controllers\UserController; Route::get("/", fn(): Response => inertia("Welcome")); -Route::get('/auth/redirect', function () { - return Socialite::driver('github')->redirect(); -}); -Route::get('/auth/callback', [UserController::class, 'login']); \ No newline at end of file +Route::get("/auth/redirect", [GithubController::class, "redirect"]); +Route::get("/auth/callback", [GithubController::class, "callback"]);