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)
|
||
|
{
|
||
|
$authorization = $request->getDto();
|
||
|
$result = $this->authService->authorization($authorization);
|
||
|
if (!$result->isSuccess()) {
|
||
|
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'));
|
||
|
}
|
||
|
}
|