Files
newznab-tmux-NNTmux/app/Support/Auth/AuthenticatesUsers.php
T
2026-02-27 16:32:54 +01:00

116 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Support\Auth;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;
trait AuthenticatesUsers
{
use RedirectsUsers, ThrottlesLogins;
/**
* Show the application's login form.
*
* @return \Illuminate\Contracts\View\View
*/
public function showLoginForm(): mixed
{
return view('auth.login');
}
/**
* Validate the user login request.
*
* @throws ValidationException
*/
protected function validateLogin(Request $request): void
{
$request->validate([
$this->username() => 'required|string',
'password' => 'required|string',
]);
}
/**
* Attempt to log the user into the application.
*/
protected function attemptLogin(Request $request): bool
{
return $this->guard()->attempt(
$this->credentials($request), $request->boolean('remember')
);
}
/**
* Get the needed authorization credentials from the request.
*
* @return array<string, mixed>
*/
protected function credentials(Request $request): array
{
return $request->only($this->username(), 'password');
}
/**
* Send the response after the user was authenticated.
*/
protected function sendLoginResponse(Request $request): \Illuminate\Http\RedirectResponse|JsonResponse
{
$request->session()->regenerate();
$this->clearLoginAttempts($request);
if ($response = $this->authenticated($request, $this->guard()->user())) {
return $response;
}
return $request->wantsJson()
? new JsonResponse([], 204)
: redirect()->intended($this->redirectPath());
}
/**
* The user has been authenticated.
*
* @param mixed $user
* @return \Illuminate\Http\RedirectResponse|JsonResponse|null
*/
protected function authenticated(Request $request, $user): mixed
{
return null;
}
/**
* Get the failed login response instance.
*
* @throws ValidationException
*/
protected function sendFailedLoginResponse(Request $request): never
{
throw ValidationException::withMessages([
$this->username() => [trans('auth.failed')],
]);
}
/**
* Get the login username to be used by the controller.
*/
public function username(): string
{
return 'email';
}
/**
* Get the guard to be used during authentication.
*/
protected function guard(): \Illuminate\Contracts\Auth\StatefulGuard
{
return Auth::guard();
}
}