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