Skip to content

Commit 87fe12e

Browse files
committed
Replace register with fortify
1 parent 3acdc7a commit 87fe12e

File tree

9 files changed

+84
-89
lines changed

9 files changed

+84
-89
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace App\Actions\Fortify;
4+
5+
use App\Models\User;
6+
use Illuminate\Support\Facades\Hash;
7+
use Illuminate\Support\Facades\Validator;
8+
use Illuminate\Validation\Rule;
9+
use Laravel\Fortify\Contracts\CreatesNewUsers;
10+
11+
class CreateNewUser implements CreatesNewUsers
12+
{
13+
use PasswordValidationRules;
14+
15+
/**
16+
* Validate and create a newly registered user.
17+
*
18+
* @param array<string, string> $input
19+
*/
20+
public function create(array $input): User
21+
{
22+
Validator::make($input, [
23+
'name' => ['required', 'string', 'max:255'],
24+
'email' => [
25+
'required',
26+
'string',
27+
'email',
28+
'max:255',
29+
Rule::unique(User::class),
30+
],
31+
'password' => $this->passwordRules(),
32+
])->validate();
33+
34+
return User::create([
35+
'name' => $input['name'],
36+
'email' => $input['email'],
37+
'password' => Hash::make($input['password']),
38+
]);
39+
}
40+
}

app/Http/Controllers/Auth/RegisteredUserController.php

Lines changed: 0 additions & 53 deletions
This file was deleted.

app/Providers/FortifyServiceProvider.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Providers;
44

5+
use App\Actions\Fortify\CreateNewUser;
56
use App\Actions\Fortify\ResetUserPassword;
67
use Illuminate\Cache\RateLimiting\Limit;
78
use Illuminate\Http\Request;
@@ -38,6 +39,7 @@ public function boot(): void
3839
private function configureActions(): void
3940
{
4041
Fortify::resetUserPasswordsUsing(ResetUserPassword::class);
42+
Fortify::createUsersUsing(CreateNewUser::class);
4143
}
4244

4345
/**
@@ -47,22 +49,25 @@ private function configureViews(): void
4749
{
4850
Fortify::loginView(fn (Request $request) => Inertia::render('auth/login', [
4951
'canResetPassword' => Features::enabled(Features::resetPasswords()),
52+
'canRegister' => Features::enabled(Features::registration()),
5053
'status' => $request->session()->get('status'),
5154
]));
5255

53-
Fortify::verifyEmailView(fn (Request $request) => Inertia::render('auth/verify-email', [
54-
'status' => $request->session()->get('status'),
56+
Fortify::resetPasswordView(fn (Request $request) => Inertia::render('auth/reset-password', [
57+
'email' => $request->email,
58+
'token' => $request->route('token'),
5559
]));
5660

5761
Fortify::requestPasswordResetLinkView(fn (Request $request) => Inertia::render('auth/forgot-password', [
5862
'status' => $request->session()->get('status'),
5963
]));
6064

61-
Fortify::resetPasswordView(fn (Request $request) => Inertia::render('auth/reset-password', [
62-
'email' => $request->email,
63-
'token' => $request->route('token'),
65+
Fortify::verifyEmailView(fn (Request $request) => Inertia::render('auth/verify-email', [
66+
'status' => $request->session()->get('status'),
6467
]));
6568

69+
Fortify::registerView(fn () => Inertia::render('auth/register'));
70+
6671
Fortify::twoFactorChallengeView(fn () => Inertia::render('auth/two-factor-challenge'));
6772

6873
Fortify::confirmPasswordView(fn () => Inertia::render('auth/confirm-password'));

config/fortify.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
*/
145145

146146
'features' => [
147-
// Features::registration(),
147+
Features::registration(),
148148
Features::resetPasswords(),
149149
Features::emailVerification(),
150150
Features::twoFactorAuthentication([

resources/js/pages/auth/login.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ import { LoaderCircle } from 'lucide-react';
1414
interface LoginProps {
1515
status?: string;
1616
canResetPassword: boolean;
17+
canRegister: boolean;
1718
}
1819

19-
export default function Login({ status, canResetPassword }: LoginProps) {
20+
export default function Login({
21+
status,
22+
canResetPassword,
23+
canRegister,
24+
}: LoginProps) {
2025
return (
2126
<AuthLayout
2227
title="Log in to your account"
@@ -95,12 +100,14 @@ export default function Login({ status, canResetPassword }: LoginProps) {
95100
</Button>
96101
</div>
97102

98-
<div className="text-center text-sm text-muted-foreground">
99-
Don't have an account?{' '}
100-
<TextLink href={register()} tabIndex={5}>
101-
Sign up
102-
</TextLink>
103-
</div>
103+
{canRegister && (
104+
<div className="text-center text-sm text-muted-foreground">
105+
Don't have an account?{' '}
106+
<TextLink href={register()} tabIndex={5}>
107+
Sign up
108+
</TextLink>
109+
</div>
110+
)}
104111
</>
105112
)}
106113
</Form>

resources/js/pages/auth/register.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import RegisteredUserController from '@/actions/App/Http/Controllers/Auth/RegisteredUserController';
21
import { login } from '@/routes';
2+
import { store } from '@/routes/register';
33
import { Form, Head } from '@inertiajs/react';
44
import { LoaderCircle } from 'lucide-react';
55

@@ -18,7 +18,7 @@ export default function Register() {
1818
>
1919
<Head title="Register" />
2020
<Form
21-
{...RegisteredUserController.store.form()}
21+
{...store.form()}
2222
resetOnSuccess={['password', 'password_confirmation']}
2323
disableWhileProcessing
2424
className="flex flex-col gap-6"

resources/js/pages/welcome.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { dashboard, login, register } from '@/routes';
22
import { type SharedData } from '@/types';
33
import { Head, Link, usePage } from '@inertiajs/react';
44

5-
export default function Welcome() {
5+
export default function Welcome({
6+
canRegister = true,
7+
}: {
8+
canRegister?: boolean;
9+
}) {
610
const { auth } = usePage<SharedData>().props;
711

812
return (
@@ -32,12 +36,14 @@ export default function Welcome() {
3236
>
3337
Log in
3438
</Link>
35-
<Link
36-
href={register()}
37-
className="inline-block rounded-sm border border-[#19140035] px-5 py-1.5 text-sm leading-normal text-[#1b1b18] hover:border-[#1915014a] dark:border-[#3E3E3A] dark:text-[#EDEDEC] dark:hover:border-[#62605b]"
38-
>
39-
Register
40-
</Link>
39+
{canRegister && (
40+
<Link
41+
href={register()}
42+
className="inline-block rounded-sm border border-[#19140035] px-5 py-1.5 text-sm leading-normal text-[#1b1b18] hover:border-[#1915014a] dark:border-[#3E3E3A] dark:text-[#EDEDEC] dark:hover:border-[#62605b]"
43+
>
44+
Register
45+
</Link>
46+
)}
4147
</>
4248
)}
4349
</nav>

routes/auth.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

routes/web.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
use Illuminate\Support\Facades\Route;
44
use Inertia\Inertia;
5+
use Laravel\Fortify\Features;
56

67
Route::get('/', function () {
7-
return Inertia::render('welcome');
8+
return Inertia::render('welcome', [
9+
'canRegister' => Features::enabled(Features::registration()),
10+
]);
811
})->name('home');
912

1013
Route::middleware(['auth', 'verified'])->group(function () {
@@ -14,4 +17,3 @@
1417
});
1518

1619
require __DIR__.'/settings.php';
17-
require __DIR__.'/auth.php';

0 commit comments

Comments
 (0)