ldc-gen-game-web/app/Http/Controllers/Fo/Auth/LoginController.php
2026-10-20 12:27:49 +02:00

101 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Fo\Auth;
use App\Http\Controllers\Controller;
use App\Models\Customer;
use App\Models\Settings;
use App\Providers\RouteServiceProvider;
use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
class LoginController extends Controller
{
use AuthenticatesUsers;
use ThrottlesLogins;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
/**
* Create a new controller instance.
*/
public function __construct()
{
$this->middleware('guest:frontend')->except('logout');
}
/**
* Get the guard to be used during authentication.
*/
protected function guard(): StatefulGuard
{
return Auth::guard('frontend');
}
/**
* Show the application's login form.
*
* @return \Illuminate\View\View
*/
public function showLoginForm(): View
{
/** @var view-string $view */
$view = 'theme::auth.login';
return view($view);
}
/**
* Check if the authenticated user can access the game
*
* @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing, Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
*/
protected function authenticated(Request $request, Customer $user): void
{
/** @var \LDC\LDCCRM\Models\Customer $crmCustomer */
$crmCustomer = $user->api_data;
/** @var Settings */
$settings = resolve(Settings::class);
$countries = $settings->authorized_countries;
if (count($countries) && ! in_array($crmCustomer->country->value, $countries)) {
$this->logout($request);
Session::flash('info', trans('front.messages.game_not_available_in_country'));
} else {
$user->last_connection = \now();
$user->saveOrFail();
Session::flash('info', trans('front.messages.login_success'));
}
}
/**
* The user has logged out of the application.
*
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
*/
protected function loggedOut(Request $request): void
{
Session::flash('info', trans('front.messages.logout_success'));
}
/**
* Get the post register / login redirect path.
*/
public function redirectPath(): string
{
return response()->redirectToIntended()->getTargetUrl();
}
}