From a777e8cde7c2df28f2d26b2b8c1ae2fcf435b701 Mon Sep 17 00:00:00 2001 From: Pushpak Chhajed Date: Tue, 14 Oct 2025 17:31:00 +0530 Subject: [PATCH 1/3] Replace register with fortify --- app/Actions/Fortify/CreateNewUser.php | 40 ++++++++++++++ .../Auth/RegisteredUserController.php | 53 ------------------- app/Providers/FortifyServiceProvider.php | 15 ++++-- config/fortify.php | 2 +- resources/js/pages/auth/login.tsx | 21 +++++--- resources/js/pages/auth/register.tsx | 4 +- resources/js/pages/welcome.tsx | 20 ++++--- routes/auth.php | 12 ----- routes/web.php | 6 ++- 9 files changed, 84 insertions(+), 89 deletions(-) create mode 100644 app/Actions/Fortify/CreateNewUser.php delete mode 100644 app/Http/Controllers/Auth/RegisteredUserController.php delete mode 100644 routes/auth.php diff --git a/app/Actions/Fortify/CreateNewUser.php b/app/Actions/Fortify/CreateNewUser.php new file mode 100644 index 00000000..3d59821e --- /dev/null +++ b/app/Actions/Fortify/CreateNewUser.php @@ -0,0 +1,40 @@ + $input + */ + public function create(array $input): User + { + Validator::make($input, [ + 'name' => ['required', 'string', 'max:255'], + 'email' => [ + 'required', + 'string', + 'email', + 'max:255', + Rule::unique(User::class), + ], + 'password' => $this->passwordRules(), + ])->validate(); + + return User::create([ + 'name' => $input['name'], + 'email' => $input['email'], + 'password' => $input['password'], + ]); + } +} diff --git a/app/Http/Controllers/Auth/RegisteredUserController.php b/app/Http/Controllers/Auth/RegisteredUserController.php deleted file mode 100644 index c1286837..00000000 --- a/app/Http/Controllers/Auth/RegisteredUserController.php +++ /dev/null @@ -1,53 +0,0 @@ -validate([ - 'name' => 'required|string|max:255', - 'email' => 'required|string|lowercase|email|max:255|unique:'.User::class, - 'password' => ['required', 'confirmed', Rules\Password::defaults()], - ]); - - $user = User::create([ - 'name' => $request->name, - 'email' => $request->email, - 'password' => Hash::make($request->password), - ]); - - event(new Registered($user)); - - Auth::login($user); - - $request->session()->regenerate(); - - return redirect()->intended(route('dashboard', absolute: false)); - } -} diff --git a/app/Providers/FortifyServiceProvider.php b/app/Providers/FortifyServiceProvider.php index 961914ce..968fe15c 100644 --- a/app/Providers/FortifyServiceProvider.php +++ b/app/Providers/FortifyServiceProvider.php @@ -2,6 +2,7 @@ namespace App\Providers; +use App\Actions\Fortify\CreateNewUser; use App\Actions\Fortify\ResetUserPassword; use Illuminate\Cache\RateLimiting\Limit; use Illuminate\Http\Request; @@ -38,6 +39,7 @@ public function boot(): void private function configureActions(): void { Fortify::resetUserPasswordsUsing(ResetUserPassword::class); + Fortify::createUsersUsing(CreateNewUser::class); } /** @@ -47,22 +49,25 @@ private function configureViews(): void { Fortify::loginView(fn (Request $request) => Inertia::render('auth/login', [ 'canResetPassword' => Features::enabled(Features::resetPasswords()), + 'canRegister' => Features::enabled(Features::registration()), 'status' => $request->session()->get('status'), ])); - Fortify::verifyEmailView(fn (Request $request) => Inertia::render('auth/verify-email', [ - 'status' => $request->session()->get('status'), + Fortify::resetPasswordView(fn (Request $request) => Inertia::render('auth/reset-password', [ + 'email' => $request->email, + 'token' => $request->route('token'), ])); Fortify::requestPasswordResetLinkView(fn (Request $request) => Inertia::render('auth/forgot-password', [ 'status' => $request->session()->get('status'), ])); - Fortify::resetPasswordView(fn (Request $request) => Inertia::render('auth/reset-password', [ - 'email' => $request->email, - 'token' => $request->route('token'), + Fortify::verifyEmailView(fn (Request $request) => Inertia::render('auth/verify-email', [ + 'status' => $request->session()->get('status'), ])); + Fortify::registerView(fn () => Inertia::render('auth/register')); + Fortify::twoFactorChallengeView(fn () => Inertia::render('auth/two-factor-challenge')); Fortify::confirmPasswordView(fn () => Inertia::render('auth/confirm-password')); diff --git a/config/fortify.php b/config/fortify.php index 9dd5999b..ce67e2c3 100644 --- a/config/fortify.php +++ b/config/fortify.php @@ -144,7 +144,7 @@ */ 'features' => [ - // Features::registration(), + Features::registration(), Features::resetPasswords(), Features::emailVerification(), Features::twoFactorAuthentication([ diff --git a/resources/js/pages/auth/login.tsx b/resources/js/pages/auth/login.tsx index 7fff5f54..beda09eb 100644 --- a/resources/js/pages/auth/login.tsx +++ b/resources/js/pages/auth/login.tsx @@ -14,9 +14,14 @@ import { LoaderCircle } from 'lucide-react'; interface LoginProps { status?: string; canResetPassword: boolean; + canRegister: boolean; } -export default function Login({ status, canResetPassword }: LoginProps) { +export default function Login({ + status, + canResetPassword, + canRegister, +}: LoginProps) { return ( -
- Don't have an account?{' '} - - Sign up - -
+ {canRegister && ( +
+ Don't have an account?{' '} + + Sign up + +
+ )} )} diff --git a/resources/js/pages/auth/register.tsx b/resources/js/pages/auth/register.tsx index 4015acae..96a44ef9 100644 --- a/resources/js/pages/auth/register.tsx +++ b/resources/js/pages/auth/register.tsx @@ -1,5 +1,5 @@ -import RegisteredUserController from '@/actions/App/Http/Controllers/Auth/RegisteredUserController'; import { login } from '@/routes'; +import { store } from '@/routes/register'; import { Form, Head } from '@inertiajs/react'; import { LoaderCircle } from 'lucide-react'; @@ -18,7 +18,7 @@ export default function Register() { >
().props; return ( @@ -32,12 +36,14 @@ export default function Welcome() { > Log in - - Register - + {canRegister && ( + + Register + + )} )} diff --git a/routes/auth.php b/routes/auth.php deleted file mode 100644 index a03d112d..00000000 --- a/routes/auth.php +++ /dev/null @@ -1,12 +0,0 @@ -group(function () { - Route::get('register', [RegisteredUserController::class, 'create']) - ->name('register'); - - Route::post('register', [RegisteredUserController::class, 'store']) - ->name('register.store'); -}); diff --git a/routes/web.php b/routes/web.php index 5e4cebdf..11e61c22 100644 --- a/routes/web.php +++ b/routes/web.php @@ -2,9 +2,12 @@ use Illuminate\Support\Facades\Route; use Inertia\Inertia; +use Laravel\Fortify\Features; Route::get('/', function () { - return Inertia::render('welcome'); + return Inertia::render('welcome', [ + 'canRegister' => Features::enabled(Features::registration()), + ]); })->name('home'); Route::middleware(['auth', 'verified'])->group(function () { @@ -14,4 +17,3 @@ }); require __DIR__.'/settings.php'; -require __DIR__.'/auth.php'; From 55a66951ff3837f5142924bb61029259d76727d2 Mon Sep 17 00:00:00 2001 From: Pushpak Chhajed Date: Mon, 20 Oct 2025 20:10:23 +0530 Subject: [PATCH 2/3] Add Hash pasword --- app/Actions/Fortify/CreateNewUser.php | 2 +- app/Actions/Fortify/ResetUserPassword.php | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/Actions/Fortify/CreateNewUser.php b/app/Actions/Fortify/CreateNewUser.php index 3d59821e..7bf18d0a 100644 --- a/app/Actions/Fortify/CreateNewUser.php +++ b/app/Actions/Fortify/CreateNewUser.php @@ -34,7 +34,7 @@ public function create(array $input): User return User::create([ 'name' => $input['name'], 'email' => $input['email'], - 'password' => $input['password'], + 'password' => Hash::make($input['password']), ]); } } diff --git a/app/Actions/Fortify/ResetUserPassword.php b/app/Actions/Fortify/ResetUserPassword.php index 688d62f3..7a57c503 100644 --- a/app/Actions/Fortify/ResetUserPassword.php +++ b/app/Actions/Fortify/ResetUserPassword.php @@ -3,6 +3,7 @@ namespace App\Actions\Fortify; use App\Models\User; +use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Validator; use Laravel\Fortify\Contracts\ResetsUserPasswords; @@ -22,7 +23,7 @@ public function reset(User $user, array $input): void ])->validate(); $user->forceFill([ - 'password' => $input['password'], + 'password' => Hash::make($input['password']), ])->save(); } } From 55cf6124ff99ae144c615ec2e72b0e7a25c0adb8 Mon Sep 17 00:00:00 2001 From: Joe Tannenbaum Date: Mon, 20 Oct 2025 10:48:53 -0400 Subject: [PATCH 3/3] remove unnecessary hash call --- app/Actions/Fortify/CreateNewUser.php | 3 +-- app/Actions/Fortify/ResetUserPassword.php | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/app/Actions/Fortify/CreateNewUser.php b/app/Actions/Fortify/CreateNewUser.php index 7bf18d0a..aebff5e4 100644 --- a/app/Actions/Fortify/CreateNewUser.php +++ b/app/Actions/Fortify/CreateNewUser.php @@ -3,7 +3,6 @@ namespace App\Actions\Fortify; use App\Models\User; -use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Validator; use Illuminate\Validation\Rule; use Laravel\Fortify\Contracts\CreatesNewUsers; @@ -34,7 +33,7 @@ public function create(array $input): User return User::create([ 'name' => $input['name'], 'email' => $input['email'], - 'password' => Hash::make($input['password']), + 'password' => $input['password'], ]); } } diff --git a/app/Actions/Fortify/ResetUserPassword.php b/app/Actions/Fortify/ResetUserPassword.php index 7a57c503..688d62f3 100644 --- a/app/Actions/Fortify/ResetUserPassword.php +++ b/app/Actions/Fortify/ResetUserPassword.php @@ -3,7 +3,6 @@ namespace App\Actions\Fortify; use App\Models\User; -use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Validator; use Laravel\Fortify\Contracts\ResetsUserPasswords; @@ -23,7 +22,7 @@ public function reset(User $user, array $input): void ])->validate(); $user->forceFill([ - 'password' => Hash::make($input['password']), + 'password' => $input['password'], ])->save(); } }