Leonid Nikitin
92206a028a
Changed the error check in the AuthController from checking if the result is not successful to checking if the result has an error. This change was made to clarify the code and ensure that errors are handled properly.
48 lines
1.5 KiB
PHP
48 lines
1.5 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\AuthorizationRequest;
|
|
use App\Services\AuthService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\View\View;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
final class AuthController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly AuthService $authService
|
|
) { }
|
|
|
|
public function login(): View
|
|
{
|
|
return view('public/login');
|
|
}
|
|
|
|
public function authorization(AuthorizationRequest $request): RedirectResponse
|
|
{
|
|
$authorization = $request->getDto();
|
|
$result = $this->authService->authorization($authorization);
|
|
if ($result->isError()) {
|
|
if ($result->getCode() === Response::HTTP_UNAUTHORIZED) {
|
|
Log::warning('Unauthorized ' . $authorization->getEmail() . ' [' . $request->getClientIp() . ']');
|
|
}
|
|
return redirect()->route('login')->withInput()->withErrors($result->getMessage());
|
|
}
|
|
$request->session()->regenerate();
|
|
Log::notice('Logged in ' . $authorization->getEmail() . ' [' . $request->getClientIp() . ']');
|
|
return redirect()->route('home');
|
|
}
|
|
|
|
public function logout(Request $request): RedirectResponse
|
|
{
|
|
Auth::logout();
|
|
$request->session()->invalidate();
|
|
$request->session()->regenerateToken();
|
|
return redirect(route('login'));
|
|
}
|
|
}
|