Path: blob/1.0-develop/app/Http/Controllers/Auth/AbstractLoginController.php
10284 views
<?php12namespace Pterodactyl\Http\Controllers\Auth;34use Illuminate\Http\Request;5use Pterodactyl\Models\User;6use Illuminate\Auth\AuthManager;7use Illuminate\Http\JsonResponse;8use Illuminate\Auth\Events\Failed;9use Illuminate\Container\Container;10use Illuminate\Support\Facades\Event;11use Pterodactyl\Events\Auth\DirectLogin;12use Pterodactyl\Exceptions\DisplayException;13use Pterodactyl\Http\Controllers\Controller;14use Illuminate\Contracts\Auth\Authenticatable;15use Illuminate\Foundation\Auth\AuthenticatesUsers;1617abstract class AbstractLoginController extends Controller18{19use AuthenticatesUsers;2021protected AuthManager $auth;2223/**24* Lockout time for failed login requests.25*/26protected int $lockoutTime;2728/**29* After how many attempts should logins be throttled and locked.30*/31protected int $maxLoginAttempts;3233/**34* Where to redirect users after login / registration.35*/36protected string $redirectTo = '/';3738/**39* LoginController constructor.40*/41public function __construct()42{43$this->lockoutTime = config('auth.lockout.time');44$this->maxLoginAttempts = config('auth.lockout.attempts');45$this->auth = Container::getInstance()->make(AuthManager::class);46}4748/**49* Get the failed login response instance.50*51* @return never-return52*53* @throws DisplayException54*/55protected function sendFailedLoginResponse(Request $request, ?Authenticatable $user = null, ?string $message = null)56{57$this->incrementLoginAttempts($request);58$this->fireFailedLoginEvent($user, [59$this->getField($request->input('user')) => $request->input('user'),60]);6162if ($request->route()->named('auth.login-checkpoint')) {63throw new DisplayException($message ?? trans('auth.two_factor.checkpoint_failed'));64}6566throw new DisplayException(trans('auth.failed'));67}6869/**70* Send the response after the user was authenticated.71*/72protected function sendLoginResponse(User $user, Request $request): JsonResponse73{74$request->session()->remove('auth_confirmation_token');75$request->session()->regenerate();7677$this->clearLoginAttempts($request);7879$this->auth->guard()->login($user, true);8081Event::dispatch(new DirectLogin($user, true));8283return new JsonResponse([84'data' => [85'complete' => true,86'intended' => $this->redirectPath(),87'user' => $user->toVueObject(),88],89]);90}9192/**93* Determine if the user is logging in using an email or username.94*/95protected function getField(?string $input = null): string96{97return ($input && str_contains($input, '@')) ? 'email' : 'username';98}99100/**101* Fire a failed login event.102*/103protected function fireFailedLoginEvent(?Authenticatable $user = null, array $credentials = [])104{105Event::dispatch(new Failed('auth', $user, $credentials));106}107}108109110