Reorganize setting items

This commit is contained in:
Víctor Falcón 2025-11-07 18:23:08 +00:00
parent 689666e0f5
commit 499c11eb6d
8 changed files with 492 additions and 20 deletions

View File

@ -10,9 +10,23 @@ use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Inertia\Inertia;
use Inertia\Response;
use Laravel\Fortify\Features;
class ProfileController extends Controller
{
/**
* Show the combined account settings page.
*/
public function account(Request $request): Response
{
return Inertia::render('settings/account', [
'mustVerifyEmail' => $request->user() instanceof MustVerifyEmail,
'status' => $request->session()->get('status'),
'twoFactorEnabled' => $request->user()->hasEnabledTwoFactorAuthentication(),
'requiresConfirmation' => Features::optionEnabled(Features::twoFactorAuthentication(), 'confirm'),
]);
}
/**
* Show the user's profile settings page.
*/
@ -37,7 +51,7 @@ class ProfileController extends Controller
$request->user()->save();
return to_route('profile.edit');
return to_route('account.edit');
}
/**

View File

@ -4,27 +4,16 @@ import { Separator } from '@/components/ui/separator';
import { cn, isSameUrl, resolveUrl } from '@/lib/utils';
import { edit as editAppearance } from '@/routes/appearance';
import { index as categoriesIndex } from '@/actions/App/Http/Controllers/Settings/CategoryController';
import { edit } from '@/routes/profile';
import { show } from '@/routes/two-factor';
import { edit as editPassword } from '@/routes/user-password';
import { edit as editAccount } from '@/routes/account';
import { edit as editDeleteAccount } from '@/routes/delete-account';
import { type NavItem } from '@/types';
import { Link } from '@inertiajs/react';
import { type PropsWithChildren } from 'react';
const sidebarNavItems: NavItem[] = [
{
title: 'Profile',
href: edit(),
icon: null,
},
{
title: 'Password',
href: editPassword(),
icon: null,
},
{
title: 'Two-Factor Auth',
href: show(),
title: 'Account',
href: editAccount(),
icon: null,
},
{
@ -37,6 +26,11 @@ const sidebarNavItems: NavItem[] = [
href: editAppearance(),
icon: null,
},
{
title: 'Delete Account',
href: editDeleteAccount(),
icon: null,
},
];
export default function SettingsLayout({ children }: PropsWithChildren) {

View File

@ -0,0 +1,376 @@
import PasswordController from '@/actions/App/Http/Controllers/Settings/PasswordController';
import ProfileController from '@/actions/App/Http/Controllers/Settings/ProfileController';
import HeadingSmall from '@/components/heading-small';
import InputError from '@/components/input-error';
import TwoFactorRecoveryCodes from '@/components/two-factor-recovery-codes';
import TwoFactorSetupModal from '@/components/two-factor-setup-modal';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Separator } from '@/components/ui/separator';
import { useTwoFactorAuth } from '@/hooks/use-two-factor-auth';
import AppLayout from '@/layouts/app-layout';
import SettingsLayout from '@/layouts/settings/layout';
import { edit as accountEdit } from '@/routes/account';
import { disable, enable } from '@/routes/two-factor';
import { send } from '@/routes/verification';
import { type BreadcrumbItem, type SharedData } from '@/types';
import { Transition } from '@headlessui/react';
import { Form, Head, Link, usePage } from '@inertiajs/react';
import { ShieldBan, ShieldCheck } from 'lucide-react';
import { useRef, useState } from 'react';
const breadcrumbs: BreadcrumbItem[] = [
{
title: 'Account settings',
href: accountEdit().url,
},
];
export default function Account({
mustVerifyEmail,
status,
requiresConfirmation = false,
twoFactorEnabled = false,
}: {
mustVerifyEmail: boolean;
status?: string;
requiresConfirmation?: boolean;
twoFactorEnabled?: boolean;
}) {
const { auth } = usePage<SharedData>().props;
const passwordInput = useRef<HTMLInputElement>(null);
const currentPasswordInput = useRef<HTMLInputElement>(null);
const {
qrCodeSvg,
hasSetupData,
manualSetupKey,
clearSetupData,
fetchSetupData,
recoveryCodesList,
fetchRecoveryCodes,
errors: twoFactorErrors,
} = useTwoFactorAuth();
const [showSetupModal, setShowSetupModal] = useState<boolean>(false);
return (
<AppLayout breadcrumbs={breadcrumbs}>
<Head title="Account settings" />
<SettingsLayout>
<div className="space-y-6">
<HeadingSmall
title="Profile information"
description="Update your name and email address"
/>
<Form
{...ProfileController.update.form()}
options={{
preserveScroll: true,
}}
className="space-y-6"
>
{({ processing, recentlySuccessful, errors }) => (
<>
<div className="grid gap-2">
<Label htmlFor="name">Name</Label>
<Input
id="name"
className="mt-1 block w-full"
defaultValue={auth.user.name}
name="name"
required
autoComplete="name"
placeholder="Full name"
/>
<InputError
className="mt-2"
message={errors.name}
/>
</div>
<div className="grid gap-2">
<Label htmlFor="email">Email address</Label>
<Input
id="email"
type="email"
className="mt-1 block w-full"
defaultValue={auth.user.email}
name="email"
required
autoComplete="username"
placeholder="Email address"
/>
<InputError
className="mt-2"
message={errors.email}
/>
</div>
{mustVerifyEmail &&
auth.user.email_verified_at === null && (
<div>
<p className="-mt-4 text-sm text-muted-foreground">
Your email address is
unverified.{' '}
<Link
href={send()}
as="button"
className="text-foreground underline decoration-neutral-300 underline-offset-4 transition-colors duration-300 ease-out hover:decoration-current! dark:decoration-neutral-500"
>
Click here to resend the
verification email.
</Link>
</p>
{status ===
'verification-link-sent' && (
<div className="mt-2 text-sm font-medium text-green-600">
A new verification link has
been sent to your email
address.
</div>
)}
</div>
)}
<div className="flex items-center gap-4">
<Button
disabled={processing}
data-test="update-profile-button"
>
Save
</Button>
<Transition
show={recentlySuccessful}
enter="transition ease-in-out"
enterFrom="opacity-0"
leave="transition ease-in-out"
leaveTo="opacity-0"
>
<p className="text-sm text-neutral-600">
Saved
</p>
</Transition>
</div>
</>
)}
</Form>
</div>
<Separator />
<div className="space-y-6">
<HeadingSmall
title="Update password"
description="Ensure your account is using a long, random password to stay secure"
/>
<Form
{...PasswordController.update.form()}
options={{
preserveScroll: true,
}}
resetOnError={[
'password',
'password_confirmation',
'current_password',
]}
resetOnSuccess
onError={(errors) => {
if (errors.password) {
passwordInput.current?.focus();
}
if (errors.current_password) {
currentPasswordInput.current?.focus();
}
}}
className="space-y-6"
>
{({ errors, processing, recentlySuccessful }) => (
<>
<div className="grid gap-2">
<Label htmlFor="current_password">
Current password
</Label>
<Input
id="current_password"
ref={currentPasswordInput}
name="current_password"
type="password"
className="mt-1 block w-full"
autoComplete="current-password"
placeholder="Current password"
/>
<InputError
message={errors.current_password}
/>
</div>
<div className="grid gap-2">
<Label htmlFor="password">
New password
</Label>
<Input
id="password"
ref={passwordInput}
name="password"
type="password"
className="mt-1 block w-full"
autoComplete="new-password"
placeholder="New password"
/>
<InputError message={errors.password} />
</div>
<div className="grid gap-2">
<Label htmlFor="password_confirmation">
Confirm password
</Label>
<Input
id="password_confirmation"
name="password_confirmation"
type="password"
className="mt-1 block w-full"
autoComplete="new-password"
placeholder="Confirm password"
/>
<InputError
message={errors.password_confirmation}
/>
</div>
<div className="flex items-center gap-4">
<Button
disabled={processing}
data-test="update-password-button"
>
Save password
</Button>
<Transition
show={recentlySuccessful}
enter="transition ease-in-out"
enterFrom="opacity-0"
leave="transition ease-in-out"
leaveTo="opacity-0"
>
<p className="text-sm text-neutral-600">
Saved
</p>
</Transition>
</div>
</>
)}
</Form>
</div>
<Separator />
<div className="space-y-6">
<HeadingSmall
title="Two-Factor Authentication"
description="Manage your two-factor authentication settings"
/>
{twoFactorEnabled ? (
<div className="flex flex-col items-start justify-start space-y-4">
<Badge variant="default">Enabled</Badge>
<p className="text-muted-foreground">
With two-factor authentication enabled, you will
be prompted for a secure, random pin during
login, which you can retrieve from the
TOTP-supported application on your phone.
</p>
<TwoFactorRecoveryCodes
recoveryCodesList={recoveryCodesList}
fetchRecoveryCodes={fetchRecoveryCodes}
errors={twoFactorErrors}
/>
<div className="relative inline">
<Form {...disable.form()}>
{({ processing }) => (
<Button
variant="destructive"
type="submit"
disabled={processing}
>
<ShieldBan /> Disable 2FA
</Button>
)}
</Form>
</div>
</div>
) : (
<div className="flex flex-col items-start justify-start space-y-4">
<Badge variant="destructive">Disabled</Badge>
<p className="text-muted-foreground">
When you enable two-factor authentication, you
will be prompted for a secure pin during login.
This pin can be retrieved from a TOTP-supported
application on your phone.
</p>
<div>
{hasSetupData ? (
<Button
onClick={() => setShowSetupModal(true)}
>
<ShieldCheck />
Continue Setup
</Button>
) : (
<Form
{...enable.form()}
onSuccess={() =>
setShowSetupModal(true)
}
>
{({ processing }) => (
<Button
type="submit"
disabled={processing}
>
<ShieldCheck />
Enable 2FA
</Button>
)}
</Form>
)}
</div>
</div>
)}
<TwoFactorSetupModal
isOpen={showSetupModal}
onClose={() => setShowSetupModal(false)}
requiresConfirmation={requiresConfirmation}
twoFactorEnabled={twoFactorEnabled}
qrCodeSvg={qrCodeSvg}
manualSetupKey={manualSetupKey}
clearSetupData={clearSetupData}
fetchSetupData={fetchSetupData}
errors={twoFactorErrors}
/>
</div>
</SettingsLayout>
</AppLayout>
);
}

View File

@ -0,0 +1,26 @@
import DeleteUser from '@/components/delete-user';
import AppLayout from '@/layouts/app-layout';
import SettingsLayout from '@/layouts/settings/layout';
import { edit as deleteAccountEdit } from '@/routes/delete-account';
import { type BreadcrumbItem } from '@/types';
import { Head } from '@inertiajs/react';
const breadcrumbs: BreadcrumbItem[] = [
{
title: 'Delete account',
href: deleteAccountEdit().url,
},
];
export default function DeleteAccount() {
return (
<AppLayout breadcrumbs={breadcrumbs}>
<Head title="Delete account" />
<SettingsLayout>
<DeleteUser />
</SettingsLayout>
</AppLayout>
);
}

View File

@ -8,12 +8,13 @@ use Illuminate\Support\Facades\Route;
use Inertia\Inertia;
Route::middleware('auth')->group(function () {
Route::redirect('settings', '/settings/profile');
Route::redirect('settings', '/settings/account');
Route::get('settings/profile', [ProfileController::class, 'edit'])->name('profile.edit');
Route::get('settings/account', [ProfileController::class, 'account'])->name('account.edit');
Route::patch('settings/profile', [ProfileController::class, 'update'])->name('profile.update');
Route::delete('settings/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
Route::get('settings/profile', [ProfileController::class, 'edit'])->name('profile.edit');
Route::get('settings/password', [PasswordController::class, 'edit'])->name('user-password.edit');
Route::put('settings/password', [PasswordController::class, 'update'])
@ -29,6 +30,10 @@ Route::middleware('auth')->group(function () {
return Inertia::render('settings/appearance');
})->name('appearance.edit');
Route::get('settings/delete-account', function () {
return Inertia::render('settings/delete-account');
})->name('delete-account.edit');
Route::get('settings/two-factor', [TwoFactorAuthenticationController::class, 'show'])
->name('two-factor.show');
});

View File

@ -0,0 +1,30 @@
<?php
use App\Models\User;
test('account page is displayed', function () {
$user = User::factory()->create();
$response = $this
->actingAs($user)
->get(route('account.edit'));
$response->assertOk();
});
test('account page contains profile information', function () {
$user = User::factory()->create();
$response = $this
->actingAs($user)
->get(route('account.edit'));
$response
->assertOk()
->assertInertia(fn ($page) => $page
->component('settings/account')
->has('mustVerifyEmail')
->has('twoFactorEnabled')
->has('requiresConfirmation')
);
});

View File

@ -0,0 +1,27 @@
<?php
use App\Models\User;
test('delete account page is displayed', function () {
$user = User::factory()->create();
$response = $this
->actingAs($user)
->get(route('delete-account.edit'));
$response->assertOk();
});
test('delete account page displays delete account component', function () {
$user = User::factory()->create();
$response = $this
->actingAs($user)
->get(route('delete-account.edit'));
$response
->assertOk()
->assertInertia(fn ($page) => $page
->component('settings/delete-account')
);
});

View File

@ -24,7 +24,7 @@ test('profile information can be updated', function () {
$response
->assertSessionHasNoErrors()
->assertRedirect(route('profile.edit'));
->assertRedirect(route('account.edit'));
$user->refresh();
@ -45,7 +45,7 @@ test('email verification status is unchanged when the email address is unchanged
$response
->assertSessionHasNoErrors()
->assertRedirect(route('profile.edit'));
->assertRedirect(route('account.edit'));
expect($user->refresh()->email_verified_at)->not->toBeNull();
});