lasthourcloud/app/Http/Controllers/OauthController.php

47 lines
1.3 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use App\Models\InstanceSettings;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpKernel\Exception\HttpException;
2024-06-10 20:43:34 +00:00
class OauthController extends Controller
{
public function redirect(string $provider)
{
$socialite_provider = get_socialite_provider($provider);
2024-06-10 20:43:34 +00:00
return $socialite_provider->redirect();
}
public function callback(string $provider)
{
try {
$oauthUser = get_socialite_provider($provider)->user();
$user = User::whereEmail($oauthUser->email)->first();
2024-06-10 20:43:34 +00:00
if (! $user) {
$settings = InstanceSettings::get();
if (! $settings->is_registration_enabled) {
abort(403, 'Registration is disabled');
}
$user = User::create([
'name' => $oauthUser->name,
'email' => $oauthUser->email,
]);
}
Auth::login($user);
2024-06-10 20:43:34 +00:00
return redirect('/');
} catch (\Exception $e) {
ray($e->getMessage());
2024-06-10 20:43:34 +00:00
$errorCode = $e instanceof HttpException ? 'auth.failed' : 'auth.failed.callback';
return redirect()->route('login')->withErrors([__($errorCode)]);
}
}
}