-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
442 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Auth; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\User; | ||
use Exception; | ||
use Illuminate\Http\RedirectResponse; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Support\Facades\Log; | ||
use Illuminate\Support\Facades\Redirect; | ||
use Laravel\Socialite\Facades\Socialite; | ||
|
||
class GitHubSocialiteController extends Controller | ||
{ | ||
public function redirectToProvider(): RedirectResponse|Redirect | ||
{ | ||
if (! config('services.github.client_id') || ! config('services.github.client_secret')) { | ||
Log::debug('GitHub login is not enabled. Redirecting back to login.'); | ||
|
||
return Redirect::route('login')->with('loginError', 'GitHub login is not enabled.'); | ||
} | ||
|
||
return Socialite::driver('github') | ||
->scopes(['read:user']) | ||
->redirect(); | ||
} | ||
|
||
public function handleProviderCallback(): RedirectResponse | ||
{ | ||
try { | ||
$githubUser = Socialite::driver('github')->user(); | ||
|
||
if ($user = $this->findUserByGitHubId($githubUser->getId())) { | ||
return $this->loginAndRedirect($user, 'Found GH ID associated with this user, logging them in.'); | ||
} | ||
|
||
if ($user = $this->findUserByEmailAndUpdateGitHubId($githubUser)) { | ||
return $this->loginAndRedirect($user, 'Adding the user\'s GH ID to their account.'); | ||
} | ||
|
||
return $this->createUserAndLogin($githubUser); | ||
|
||
} catch (Exception $e) { | ||
Log::error('GitHub OAuth login error: ' . $e->getMessage()); | ||
|
||
return Redirect::route('login')->with('error', 'Authentication failed. There may be an error with GitHub. Please try again later.'); | ||
} | ||
} | ||
|
||
private function findUserByGitHubId(string $githubId): ?User | ||
{ | ||
return User::where('github_id', $githubId)->first(); | ||
} | ||
|
||
private function findUserByEmailAndUpdateGitHubId($githubUser): ?User | ||
{ | ||
$user = User::where('email', $githubUser->getEmail())->first(); | ||
|
||
$user?->update(['github_id' => $githubUser->getId()]); | ||
|
||
return $user; | ||
} | ||
|
||
private function createUserAndLogin($githubUser): RedirectResponse | ||
{ | ||
$user = User::create([ | ||
'name' => $githubUser->getName(), | ||
'email' => $githubUser->getEmail(), | ||
'github_id' => $githubUser->getId(), | ||
]); | ||
|
||
Log::debug('Creating new user with their GitHub ID and logging them in.', ['id' => $githubUser->getId()]); | ||
Auth::login($user); | ||
|
||
return Redirect::route('overview'); | ||
} | ||
|
||
private function loginAndRedirect(User $user, string $message): RedirectResponse | ||
{ | ||
Log::debug($message, ['id' => $user->github_id]); | ||
Auth::login($user); | ||
|
||
return Redirect::route('overview'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
database/migrations/2024_06_15_173752_make_password_nullable_in_users_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up(): void | ||
{ | ||
Schema::table('users', function (Blueprint $table) { | ||
$table->string('password')->nullable()->change(); | ||
}); | ||
} | ||
}; |
15 changes: 15 additions & 0 deletions
15
database/migrations/2024_06_15_174250_add_github_id_to_users_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up(): void | ||
{ | ||
Schema::table('users', function (Blueprint $table) { | ||
$table->string('github_id')->nullable(); | ||
}); | ||
} | ||
}; |
Oops, something went wrong.