Skip to content

Commit

Permalink
Feat: added github authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislarsen authored Jun 15, 2024
2 parents 86bd9d9 + 4d0fcaf commit 227d71b
Show file tree
Hide file tree
Showing 13 changed files with 442 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ REVERB_SCHEME=https
REVERB_SERVER_HOST=127.0.0.1
REVERB_SERVER_PORT=8080

### GITHUB AUTH ###
GITHUB_CLIENT_ID=
GITHUB_CLIENT_SECRET=

### DO NOT REMOVE ###
VITE_REVERB_APP_KEY="${REVERB_APP_KEY}"
VITE_REVERB_HOST="${REVERB_HOST}"
Expand Down
86 changes: 86 additions & 0 deletions app/Http/Controllers/Auth/GitHubSocialiteController.php
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');
}
}
1 change: 1 addition & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class User extends Authenticatable
'email',
'password',
'timezone',
'github_id',
];

protected $hidden = [
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"laravel/pail": "^1.1",
"laravel/pulse": "^1.2",
"laravel/reverb": "@beta",
"laravel/socialite": "^5.14",
"laravel/tinker": "^2.9",
"league/flysystem-aws-s3-v3": "^3.0",
"livewire/livewire": "^3.4",
Expand Down
213 changes: 212 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@
'token' => env('HORIZON_TOKEN'),
],

'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect' => config('app.url') . '/auth/github/callback',
],
];
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();
});
}
};
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();
});
}
};
Loading

0 comments on commit 227d71b

Please sign in to comment.