Skip to content

Commit 5ea8cc5

Browse files
committed
Replace register with fortify
1 parent d3a8e7c commit 5ea8cc5

File tree

8 files changed

+67
-87
lines changed

8 files changed

+67
-87
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/Providers/FortifyServiceProvider.php

Lines changed: 12 additions & 7 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;
@@ -28,19 +29,15 @@ public function register(): void
2829
public function boot(): void
2930
{
3031
Fortify::resetUserPasswordsUsing(ResetUserPassword::class);
32+
Fortify::createUsersUsing(CreateNewUser::class);
3133

3234
Fortify::loginView(fn (Request $request) => Inertia::render('auth/Login', [
3335
'canResetPassword' => Features::enabled(Features::resetPasswords()),
36+
'canRegister' => Features::enabled(Features::registration()),
3437
'status' => $request->session()->get('status'),
3538
]));
3639

37-
Fortify::verifyEmailView(fn (Request $request) => Inertia::render('auth/VerifyEmail', [
38-
'status' => $request->session()->get('status'),
39-
]));
40-
41-
Fortify::twoFactorChallengeView(fn () => Inertia::render('auth/TwoFactorChallenge'));
42-
43-
Fortify::confirmPasswordView(fn () => Inertia::render('auth/ConfirmPassword'));
40+
Fortify::registerView(fn () => Inertia::render('auth/Register'));
4441

4542
Fortify::requestPasswordResetLinkView(fn (Request $request) => Inertia::render('auth/ForgotPassword', [
4643
'status' => $request->session()->get('status'),
@@ -51,6 +48,14 @@ public function boot(): void
5148
'token' => $request->route('token'),
5249
]));
5350

51+
Fortify::verifyEmailView(fn (Request $request) => Inertia::render('auth/VerifyEmail', [
52+
'status' => $request->session()->get('status'),
53+
]));
54+
55+
Fortify::twoFactorChallengeView(fn () => Inertia::render('auth/TwoFactorChallenge'));
56+
57+
Fortify::confirmPasswordView(fn () => Inertia::render('auth/ConfirmPassword'));
58+
5459
RateLimiter::for('two-factor', function (Request $request) {
5560
return Limit::perMinute(5)->by($request->session()->get('login.id'));
5661
});

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/Welcome.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<script setup lang="ts">
22
import { dashboard, login, register } from '@/routes';
33
import { Head, Link } from '@inertiajs/vue3';
4+
5+
defineProps<{
6+
canRegister: boolean;
7+
}>();
48
</script>
59

610
<template>
@@ -30,6 +34,7 @@ import { Head, Link } from '@inertiajs/vue3';
3034
Log in
3135
</Link>
3236
<Link
37+
v-if="canRegister"
3338
:href="register()"
3439
class="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]"
3540
>

resources/js/pages/auth/Login.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { LoaderCircle } from 'lucide-vue-next';
1515
defineProps<{
1616
status?: string;
1717
canResetPassword: boolean;
18+
canRegister: boolean;
1819
}>();
1920
</script>
2021

@@ -100,7 +101,10 @@ defineProps<{
100101
</Button>
101102
</div>
102103

103-
<div class="text-center text-sm text-muted-foreground">
104+
<div
105+
class="text-center text-sm text-muted-foreground"
106+
v-if="canRegister"
107+
>
104108
Don't have an account?
105109
<TextLink :href="register()" :tabindex="5">Sign up</TextLink>
106110
</div>

routes/web.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

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

67
Route::get('/', function () {
78
return Inertia::render('Welcome');
89
})->name('home');
910

1011
Route::get('dashboard', function () {
11-
return Inertia::render('Dashboard');
12+
return Inertia::render('Dashboard', [
13+
'canRegister' => Features::enabled(Features::registration()),
14+
]);
1215
})->middleware(['auth', 'verified'])->name('dashboard');
1316

1417
require __DIR__.'/settings.php';

tests/Feature/Auth/PasswordResetTest.php

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -46,42 +46,4 @@ public function test_reset_password_screen_can_be_rendered()
4646
return true;
4747
});
4848
}
49-
50-
public function test_password_can_be_reset_with_valid_token()
51-
{
52-
Notification::fake();
53-
54-
$user = User::factory()->create();
55-
56-
$this->post(route('password.email'), ['email' => $user->email]);
57-
58-
Notification::assertSentTo($user, ResetPassword::class, function ($notification) use ($user) {
59-
$response = $this->post(route('password.store'), [
60-
'token' => $notification->token,
61-
'email' => $user->email,
62-
'password' => 'password',
63-
'password_confirmation' => 'password',
64-
]);
65-
66-
$response
67-
->assertSessionHasNoErrors()
68-
->assertRedirect(route('login'));
69-
70-
return true;
71-
});
72-
}
73-
74-
public function test_password_cannot_be_reset_with_invalid_token(): void
75-
{
76-
$user = User::factory()->create();
77-
78-
$response = $this->post(route('password.store'), [
79-
'token' => 'invalid-token',
80-
'email' => $user->email,
81-
'password' => 'newpassword123',
82-
'password_confirmation' => 'newpassword123',
83-
]);
84-
85-
$response->assertSessionHasErrors('email');
86-
}
8749
}

tests/Feature/Settings/PasswordUpdateTest.php

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use App\Models\User;
66
use Illuminate\Foundation\Testing\RefreshDatabase;
7-
use Illuminate\Support\Facades\Hash;
87
use Tests\TestCase;
98

109
class PasswordUpdateTest extends TestCase
@@ -21,42 +20,4 @@ public function test_password_update_page_is_displayed()
2120

2221
$response->assertStatus(200);
2322
}
24-
25-
public function test_password_can_be_updated()
26-
{
27-
$user = User::factory()->create();
28-
29-
$response = $this
30-
->actingAs($user)
31-
->from(route('password.edit'))
32-
->put(route('password.update'), [
33-
'current_password' => 'password',
34-
'password' => 'new-password',
35-
'password_confirmation' => 'new-password',
36-
]);
37-
38-
$response
39-
->assertSessionHasNoErrors()
40-
->assertRedirect(route('password.edit'));
41-
42-
$this->assertTrue(Hash::check('new-password', $user->refresh()->password));
43-
}
44-
45-
public function test_correct_password_must_be_provided_to_update_password()
46-
{
47-
$user = User::factory()->create();
48-
49-
$response = $this
50-
->actingAs($user)
51-
->from(route('password.edit'))
52-
->put(route('password.update'), [
53-
'current_password' => 'wrong-password',
54-
'password' => 'new-password',
55-
'password_confirmation' => 'new-password',
56-
]);
57-
58-
$response
59-
->assertSessionHasErrors('current_password')
60-
->assertRedirect(route('password.edit'));
61-
}
6223
}

0 commit comments

Comments
 (0)