Use custom code and remove laravel/ui

This commit is contained in:
DariusIII
2026-02-27 16:32:54 +01:00
parent d428e03583
commit 012174e3fc
9 changed files with 315 additions and 82 deletions
@@ -9,9 +9,9 @@ use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\LoginLoginRequest;
use App\Models\User;
use App\Services\PasswordBreachService;
use App\Support\Auth\AuthenticatesUsers;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
@@ -43,6 +43,14 @@ class LoginController extends Controller
*/
protected string $redirectTo = '/';
/**
* Get the login username to be used (form field name; value may be email or username).
*/
public function username(): string
{
return 'username';
}
/**
* Create a new controller instance.
*
@@ -10,7 +10,7 @@ use App\Models\Invitation;
use App\Models\Settings;
use App\Models\User;
use App\Rules\ValidEmailDomain;
use Illuminate\Foundation\Auth\RegistersUsers;
use App\Support\Auth\RegistersUsers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
@@ -7,23 +7,12 @@ namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Jobs\SendPasswordResetEmail;
use App\Models\User;
use Illuminate\Foundation\Auth\ResetsPasswords;
use App\Support\Auth\RedirectsUsers;
use Illuminate\Http\Request;
class ResetPasswordController extends Controller
{
/*
|--------------------------------------------------------------------------
| Password Reset Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling password reset requests
| and uses a simple trait to include this behavior. You're free to
| explore this trait and override any methods you wish to tweak.
|
*/
use ResetsPasswords;
use RedirectsUsers;
/**
* Where to redirect users after resetting their password.
+115
View File
@@ -0,0 +1,115 @@
<?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();
}
}
+20
View File
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace App\Support\Auth;
trait RedirectsUsers
{
/**
* Get the post register / login redirect path.
*/
public function redirectPath(): string
{
if (method_exists($this, 'redirectTo')) {
return $this->redirectTo();
}
return isset($this->redirectTo) ? $this->redirectTo : '/home';
}
}
+64
View File
@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
namespace App\Support\Auth;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
trait RegistersUsers
{
use RedirectsUsers;
/**
* Show the application registration form.
*
* @return \Illuminate\Contracts\View\View
*/
public function showRegistrationForm(): mixed
{
return view('auth.register');
}
/**
* Handle a registration request for the application.
*/
public function register(Request $request): \Illuminate\Http\RedirectResponse|JsonResponse
{
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
$this->guard()->login($user);
if ($response = $this->registered($request, $user)) {
return $response;
}
return $request->wantsJson()
? new JsonResponse([], 201)
: redirect($this->redirectPath());
}
/**
* Get the guard to be used during registration.
*/
protected function guard(): \Illuminate\Contracts\Auth\StatefulGuard
{
return Auth::guard();
}
/**
* The user has been registered.
*
* @param mixed $user
* @return \Illuminate\Http\RedirectResponse|JsonResponse|null
*/
protected function registered(Request $request, $user): mixed
{
return null;
}
}
+102
View File
@@ -0,0 +1,102 @@
<?php
declare(strict_types=1);
namespace App\Support\Auth;
use Illuminate\Auth\Events\Lockout;
use Illuminate\Cache\RateLimiter;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
trait ThrottlesLogins
{
/**
* Determine if the user has too many failed login attempts.
*/
protected function hasTooManyLoginAttempts(Request $request): bool
{
return $this->limiter()->tooManyAttempts(
$this->throttleKey($request), $this->maxAttempts()
);
}
/**
* Increment the login attempts for the user.
*/
protected function incrementLoginAttempts(Request $request): void
{
$this->limiter()->hit(
$this->throttleKey($request), $this->decayMinutes() * 60
);
}
/**
* Redirect the user after determining they are locked out.
*
* @throws ValidationException
*/
protected function sendLockoutResponse(Request $request): never
{
$seconds = $this->limiter()->availableIn(
$this->throttleKey($request)
);
throw ValidationException::withMessages([
$this->username() => [trans('auth.throttle', [
'seconds' => $seconds,
'minutes' => (int) ceil($seconds / 60),
])],
])->status(Response::HTTP_TOO_MANY_REQUESTS);
}
/**
* Clear the login locks for the given user credentials.
*/
protected function clearLoginAttempts(Request $request): void
{
$this->limiter()->clear($this->throttleKey($request));
}
/**
* Fire an event when a lockout occurs.
*/
protected function fireLockoutEvent(Request $request): void
{
event(new Lockout($request));
}
/**
* Get the throttle key for the given request.
*/
protected function throttleKey(Request $request): string
{
return Str::transliterate(Str::lower($request->input($this->username()).'|'.$request->ip()));
}
/**
* Get the rate limiter instance.
*/
protected function limiter(): RateLimiter
{
return app(RateLimiter::class);
}
/**
* Get the maximum number of attempts to allow.
*/
public function maxAttempts(): int
{
return isset($this->maxAttempts) ? $this->maxAttempts : 5;
}
/**
* Get the number of minutes to throttle for.
*/
public function decayMinutes(): int
{
return isset($this->decayMinutes) ? $this->decayMinutes : 1;
}
}
+1 -3
View File
@@ -63,7 +63,6 @@
"laravel/sanctum": "^4.0",
"laravel/scout": "^10.13",
"laravel/tinker": "^2.10.1",
"laravel/ui": "^4.6",
"livewire/blaze": "^1.0",
"livewire/livewire": "^3.6",
"livewire/volt": "^1.6",
@@ -163,8 +162,7 @@
},
"scripts": {
"post-update-cmd": [
"@php artisan ide-helper:generate --ansi",
"@php artisan vendor:publish --tag=laravel-assets --ansi --force"
"@php artisan ide-helper:generate --ansi"
],
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
Generated
+1 -64
View File
@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "c32c6bf6562bff67b37af654f09b476b",
"content-hash": "8de687f72473f4ec9902d492ff625378",
"packages": [
{
"name": "aharen/omdbapi",
@@ -3315,69 +3315,6 @@
},
"time": "2026-02-06T14:12:35+00:00"
},
{
"name": "laravel/ui",
"version": "v4.6.1",
"source": {
"type": "git",
"url": "https://github.com/laravel/ui.git",
"reference": "7d6ffa38d79f19c9b3e70a751a9af845e8f41d88"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/laravel/ui/zipball/7d6ffa38d79f19c9b3e70a751a9af845e8f41d88",
"reference": "7d6ffa38d79f19c9b3e70a751a9af845e8f41d88",
"shasum": ""
},
"require": {
"illuminate/console": "^9.21|^10.0|^11.0|^12.0",
"illuminate/filesystem": "^9.21|^10.0|^11.0|^12.0",
"illuminate/support": "^9.21|^10.0|^11.0|^12.0",
"illuminate/validation": "^9.21|^10.0|^11.0|^12.0",
"php": "^8.0",
"symfony/console": "^6.0|^7.0"
},
"require-dev": {
"orchestra/testbench": "^7.35|^8.15|^9.0|^10.0",
"phpunit/phpunit": "^9.3|^10.4|^11.5"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Laravel\\Ui\\UiServiceProvider"
]
},
"branch-alias": {
"dev-master": "4.x-dev"
}
},
"autoload": {
"psr-4": {
"Laravel\\Ui\\": "src/",
"Illuminate\\Foundation\\Auth\\": "auth-backend/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Taylor Otwell",
"email": "taylor@laravel.com"
}
],
"description": "Laravel UI utilities and presets.",
"keywords": [
"laravel",
"ui"
],
"support": {
"source": "https://github.com/laravel/ui/tree/v4.6.1"
},
"time": "2025-01-28T15:15:29+00:00"
},
{
"name": "league/commonmark",
"version": "2.8.0",