65 lines
1.7 KiB
PHP
65 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Web\Auth;
|
|
|
|
use App\Events\User\LoggedIn;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\TwoFactor\TwoFactorLoginRequest;
|
|
use App\Repositories\User\UserRepository;
|
|
use App\Services\Auth\ThrottlesLogins;
|
|
use Auth;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response as InertiaResponse;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
class TwoFactorTokenController extends Controller
|
|
{
|
|
use ThrottlesLogins;
|
|
|
|
public function __construct(private readonly UserRepository $users) {}
|
|
|
|
/**
|
|
* Show Two-Factor Token form.
|
|
*/
|
|
public function show(): InertiaResponse|RedirectResponse
|
|
{
|
|
return session('auth.2fa.id')
|
|
? Inertia::render('auth/TwoFactorToken')
|
|
: redirect('login');
|
|
}
|
|
|
|
/**
|
|
* Handle Two-Factor token form submission.
|
|
*/
|
|
public function update(TwoFactorLoginRequest $request): RedirectResponse
|
|
{
|
|
$this->validate($request, ['code' => 'required']);
|
|
|
|
if (! session('auth.2fa.id')) {
|
|
return redirect('login');
|
|
}
|
|
|
|
$user = $this->users->find(
|
|
$request->session()->pull('auth.2fa.id')
|
|
);
|
|
|
|
if (! $user) {
|
|
throw new NotFoundHttpException;
|
|
}
|
|
|
|
$customRedirect = $request->session()->pull('auth.redirect_to') ?: '';
|
|
|
|
if (! $request->hasValidCode($user)) {
|
|
return redirect()->to('login'.($customRedirect ? "?to={$customRedirect}" : ''))
|
|
->withErrors(trans('auth.2fa.invalid_token'));
|
|
}
|
|
|
|
Auth::login($user);
|
|
|
|
event(new LoggedIn);
|
|
|
|
return redirect()->intended($customRedirect ?: '/');
|
|
}
|
|
}
|