Skip to content

Commit

Permalink
moved github auth functions to GithubController
Browse files Browse the repository at this point in the history
  • Loading branch information
orlowski11 committed Jun 11, 2024
1 parent f6bd7ab commit ff8f7c7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 38 deletions.
35 changes: 35 additions & 0 deletions app/Http/Controllers/GithubController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers;

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

class GithubController extends Controller
{
public function redirect()
{
return Socialite::driver("github")->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("/");
}
}
28 changes: 0 additions & 28 deletions app/Http/Controllers/UserController.php

This file was deleted.

8 changes: 4 additions & 4 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
],
];
9 changes: 3 additions & 6 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Route::get("/auth/redirect", [GithubController::class, "redirect"]);
Route::get("/auth/callback", [GithubController::class, "callback"]);

0 comments on commit ff8f7c7

Please sign in to comment.