Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OAuth login #92

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ MAINTENANCE_DRIVER=cache
CACHE_DRIVER=redis

QUEUE_CONNECTION=redis

GITHUB_CLIENT_ID=c57ff1090283884ea4c6
GITHUB_CLIENT_SECRET=2d501784b18eeb59cc4ea57e9f5ac5e61f9f2492
GITHUB_REDIRECT_URL=http://127.0.0.1:8000/oauth/github/callback
37 changes: 37 additions & 0 deletions app/Http/Controllers/OAuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Http\Controllers;

use App\Actions\Fortify\CreateNewUser;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

use Laravel\Socialite\Facades\Socialite;

use App\Models\User;
use Illuminate\Support\Lottery;

class OAuthController extends Controller {

public function redirectOAuth($provider) {
return Socialite::driver($provider)->redirect();
}

public function handleOAuthCallback($provider) {
$oAuthUser = Socialite::driver($provider)->stateless()->user();

$user = User::firstWhere('email', $oAuthUser->getEmail());

if (!$user) { // OAuth Sign Up
$user = (new CreateNewUser)->create([
'name' => $oAuthUser->getName(),
'email' => $oAuthUser->getEmail(),
'password' => 'test_password123',
]);
}

Auth::login($user, true);

return redirect()->route('home');
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"laravel/framework": "^10.10",
"laravel/jetstream": "^3.2",
"laravel/sanctum": "^3.2",
"laravel/socialite": "^5.9",
"laravel/tinker": "^2.8",
"tightenco/ziggy": "^1.0"
},
Expand Down
146 changes: 146 additions & 0 deletions 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 @@ -31,4 +31,9 @@
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],

'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect' => env('GITHUB_CALLBACK_URL', 'http://127.0.0.1:8000/oauth/github/callback'),
],
];
7 changes: 6 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Http\Controllers\OAuthController;
use App\Http\Controllers\CompetitionCRUDController;
use App\Http\Controllers\CompetitionTeamCRUDController;
use App\Http\Controllers\CVController;
Expand Down Expand Up @@ -47,14 +48,18 @@
})->name('competition');

Route::prefix('/event')->name('event')->group(function () {

Route::redirect('/', '/program');

Route::prefix('{event}')->group(function () {
Route::get('/', [EventController::class, 'show'])->name('.show');
});
});

Route::prefix('/oauth/{provider}')->middleware('guest')->controller(OAuthController::class)->name('oauth')->group(function () {
Route::get('/redirect', 'redirectOAuth')->name('.redirect');
Route::get('/callback', 'handleOAuthCallback')->name('.callback');
});

Route::middleware(['auth:sanctum', config('jetstream.auth_session'), 'verified'])->group(
function () {
Route::get('/dashboard', function () {
Expand Down
Loading