From ec3c23554db6bfe0dbef16e29c34a5568cc79793 Mon Sep 17 00:00:00 2001 From: Elyess Zouaghi Date: Fri, 27 Nov 2026 18:53:46 +0100 Subject: [PATCH 1/2] feat: setup page asks for first and last name and a friendly name that fills in from them - First name and Last name (both required) replace "Your name"; the account name joins them with one space - Friendly name replaces the optional family name field: a small script fills it with " Family" until the admin types in it; it is saved as the family name the app shows; empty gives the same suggestion - Tests: joined name, required fields, length limit, fallback, the name in sync pull; the escaping test checks only for an injected script - Docs: first-admin guide, API reference, changelog --- CHANGELOG.md | 4 ++ docs/reference/api.md | 4 +- docs/self-hosting/first-admin.md | 7 ++-- .../app/Http/Controllers/SetupController.php | 9 ++++- .../app/Http/Requests/Web/SetupRequest.php | 9 ++++- kinlore-api/resources/views/setup.blade.php | 24 +++++++++++- kinlore-api/tests/Feature/FamilyNameTest.php | 38 +++++++++++++++---- kinlore-api/tests/Feature/WebSetupTest.php | 6 ++- 8 files changed, 83 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c7c355b..72b058e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ The version numbers are explained in "Project status" in the [README](README.md) ## [Unreleased] +### Changed + +- The setup page asks for **First name** and **Last name** instead of one name, and the optional family name field is now **Friendly name**. It fills in with " Family" as the admin types, and the admin can change it to anything. It is saved as the family name, which the app shows as the server's name. + ### Added - `POST /auth/login` and `POST /auth/register` accept an optional `device_name` (up to 60 characters). It is the session `name` in `GET /auth/sessions`, so the app can show each signed-in device. Without it the name stays `mobile-app`. `POST /auth/refresh` keeps the name. diff --git a/docs/reference/api.md b/docs/reference/api.md index c6f1751..71ab7be 100644 --- a/docs/reference/api.md +++ b/docs/reference/api.md @@ -61,9 +61,9 @@ Returned by login, register and `/auth/me` (`/auth/me` adds the count fields): **First admin: web setup only.** The mobile app never creates the admin. After installing the server (with or without Docker), the owner opens `APP_URL` in a web browser: - `GET /` (web): when the instance has no user, redirects to `/setup`. Otherwise a small status page: "Kinlore is running", the server address to type in the app, and the app's sign-in steps. No user data. -- `GET /setup` (web): the setup form (name, email, an optional family name, password, password confirmation, and the setup token). When the instance already has a user: redirect to `/`. +- `GET /setup` (web): the setup form (`first_name`, `last_name`, an optional `friendly_name`, `email`, `password`, `password_confirmation`, and `setup_token`). A small script on the page fills `friendly_name` with ` Family` until the admin types in it. When the instance already has a user: redirect to `/`. - The setup token is `SETUP_TOKEN` when it is set. Without it, the server makes a random token and keeps it in `storage/app/setup-token` until the first admin exists. `php artisan kinlore:setup-token` prints it, and the Docker container prints it in its log at start. No HTTP response contains the token. -- `POST /setup` (web, CSRF-protected, throttled 5/min): creates the admin under a lock, so two parallel setups cannot both succeed. Validation errors show on the form. Success shows "Your admin account is ready" with the server address and the email to use in the app. It never shows the password. +- `POST /setup` (web, CSRF-protected, throttled 5/min): creates the admin under a lock, so two parallel setups cannot both succeed. The account name is `first_name` and `last_name` joined by one space. `friendly_name` becomes the family name (below); empty gives ` Family`. Validation errors show on the form. Success shows "Your admin account is ready" with the server address and the email to use in the app. It never shows the password. - The seeded admin (`SEED_USER_*`) also works and skips the web setup. - Behind a reverse proxy, the pages and form action use the proxy's `https` address (trusted proxies, section 10). diff --git a/docs/self-hosting/first-admin.md b/docs/self-hosting/first-admin.md index b2d80c0..c15ad02 100644 --- a/docs/self-hosting/first-admin.md +++ b/docs/self-hosting/first-admin.md @@ -31,9 +31,10 @@ The setup page always asks for a setup token. So a stranger cannot take over a n The command prints `Setup token:` and the token. If `SETUP_TOKEN` is set, it tells you to use that value. In Docker, the token is also in the log of each start: `docker compose logs api | grep "Setup token"`. 2. Open the server address in a web browser, for example `https://photos.example.com`. An empty server sends you to `/setup`. -3. Enter your name, your email address and a password two times. The password needs at least 8 characters. -4. Enter a family name, or leave the field empty. Empty gives your full name and "Family", for example "Anna Example Family". The name can have up to 60 characters. You can change it later in the app. -5. Paste the setup token and send the form. +3. Enter your **First name** and **Last name**. +4. Check the **Friendly name**. It fills in from your name as you type, for example "Anna Example Family". Change it to anything you like, for example "The Smiths": after you type in it, it no longer follows your name. Your family sees this name in the app and on invitations, and the app shows it as the name of the server. It can have up to 60 characters, and you can change it later in the app. An empty field gives the same suggestion. +5. Enter your email address and a password two times. The password needs at least 8 characters. +6. Paste the setup token and send the form. The page shows "Your admin account is ready", with the server address and the email address to use in the app. diff --git a/kinlore-api/app/Http/Controllers/SetupController.php b/kinlore-api/app/Http/Controllers/SetupController.php index 55e16e8..be109d5 100644 --- a/kinlore-api/app/Http/Controllers/SetupController.php +++ b/kinlore-api/app/Http/Controllers/SetupController.php @@ -47,7 +47,14 @@ public function store(SetupRequest $request): View|RedirectResponse return redirect('/'); } - $user = $this->authService->createFirstAdmin($request->validated()); + $data = $request->validated(); + $user = $this->authService->createFirstAdmin([ + 'name' => $data['name'], + 'email' => $data['email'], + 'password' => $data['password'], + 'family_name' => $data['friendly_name'] ?? null, + 'setup_token' => $data['setup_token'], + ]); // The new admin sees the family name; the public pages keep APP_NAME. return view('setup-done', ['instanceName' => $this->family->name(), 'email' => $user->email] + $this->pageData()); diff --git a/kinlore-api/app/Http/Requests/Web/SetupRequest.php b/kinlore-api/app/Http/Requests/Web/SetupRequest.php index 617f261..6b0920d 100644 --- a/kinlore-api/app/Http/Requests/Web/SetupRequest.php +++ b/kinlore-api/app/Http/Requests/Web/SetupRequest.php @@ -16,9 +16,13 @@ class SetupRequest extends FormRequest public function rules(): array { return [ + 'first_name' => 'required|string|max:120', + 'last_name' => 'required|string|max:120', + // Built from first_name and last_name in prepareForValidation; the account keeps one name. 'name' => 'required|string|max:255', 'email' => 'required|string|email|max:255', - 'family_name' => 'nullable|string|max:'.FamilyService::NAME_MAX_LENGTH, + // The family name the app shows. Empty: " Family", as the page suggests. + 'friendly_name' => 'nullable|string|max:'.FamilyService::NAME_MAX_LENGTH, 'password' => 'required|string|min:8|max:255|confirmed', // The value itself is checked with hash_equals in AuthService::createFirstAdmin. 'setup_token' => 'required|string|max:255', @@ -31,5 +35,8 @@ protected function prepareForValidation(): void if (is_string($this->input('email'))) { $this->merge(['email' => strtolower(trim($this->input('email')))]); } + $first = is_string($this->input('first_name')) ? $this->input('first_name') : ''; + $last = is_string($this->input('last_name')) ? $this->input('last_name') : ''; + $this->merge(['name' => trim((string) preg_replace('/\s+/', ' ', "$first $last"))]); } } diff --git a/kinlore-api/resources/views/setup.blade.php b/kinlore-api/resources/views/setup.blade.php index a3af36b..d08a236 100644 --- a/kinlore-api/resources/views/setup.blade.php +++ b/kinlore-api/resources/views/setup.blade.php @@ -4,9 +4,10 @@ @php $fields = [ - 'name' => ['label' => 'Your name', 'type' => 'text', 'autocomplete' => 'name'], + 'first_name' => ['label' => 'First name', 'type' => 'text', 'autocomplete' => 'given-name'], + 'last_name' => ['label' => 'Last name', 'type' => 'text', 'autocomplete' => 'family-name'], + 'friendly_name' => ['label' => 'Friendly name', 'type' => 'text', 'autocomplete' => 'off', 'optional' => true, 'hint' => 'Your family sees this name in the app and on invitations. It fills in from your name, for example "Anna Example Family". Change it to anything you like; you can also change it later in the app.'], 'email' => ['label' => 'Email', 'type' => 'email', 'autocomplete' => 'email'], - 'family_name' => ['label' => 'Family name (optional)', 'type' => 'text', 'autocomplete' => 'off', 'optional' => true, 'hint' => 'Your family sees this name in the app and on invitations. Leave it empty to use your name, for example "Anna Example Family". You can change it later in the app.'], 'password' => ['label' => 'Password', 'type' => 'password', 'autocomplete' => 'new-password', 'hint' => 'At least 8 characters.'], 'password_confirmation' => ['label' => 'Repeat the password', 'type' => 'password', 'autocomplete' => 'new-password'], 'setup_token' => ['label' => 'Setup token', 'type' => 'password', 'autocomplete' => 'off', 'hint' => 'The server log shows it at start. You can also run "php artisan kinlore:setup-token" on the server. If you set SETUP_TOKEN in the server settings, enter that value.'], @@ -37,4 +38,23 @@ @endforeach + @endsection diff --git a/kinlore-api/tests/Feature/FamilyNameTest.php b/kinlore-api/tests/Feature/FamilyNameTest.php index 923af8e..b154829 100644 --- a/kinlore-api/tests/Feature/FamilyNameTest.php +++ b/kinlore-api/tests/Feature/FamilyNameTest.php @@ -18,7 +18,8 @@ class FamilyNameTest extends TestCase use RefreshDatabase; private const FORM = [ - 'name' => 'Anna Example', + 'first_name' => 'Anna', + 'last_name' => 'Example', 'email' => 'anna@example.test', 'password' => 'correct horse 42', 'password_confirmation' => 'correct horse 42', @@ -31,6 +32,27 @@ protected function setUp(): void config(['app.setup_token' => 'the-secret', 'app.name' => 'Kinlore']); } + /** The name the app gets with each sync page. */ + private function actingAsAdminAndPull(): string + { + Sanctum::actingAs(User::first()); + + return $this->pulledName(); + } + + public function test_setup_joins_first_and_last_name_and_requires_both(): void + { + $this->post('/setup', ['first_name' => ' Anna ', 'last_name' => ' van Dijk '] + self::FORM)->assertOk(); + $this->assertSame('Anna van Dijk', User::first()->name); + $this->assertSame('Anna van Dijk Family', app(FamilyService::class)->name()); + + User::query()->delete(); + $this->from('/setup')->post('/setup', ['first_name' => ''] + self::FORM)->assertRedirect('/setup')->assertSessionHasErrors('first_name'); + $this->from('/setup')->post('/setup', ['last_name' => ''] + self::FORM)->assertRedirect('/setup')->assertSessionHasErrors('last_name'); + $this->from('/setup')->post('/setup', ['friendly_name' => str_repeat('x', 61)] + self::FORM)->assertSessionHasErrors('friendly_name'); + $this->assertDatabaseCount('users', 0); + } + private function pulledName(): string { return $this->postJson('/api/v1/sync/pull', ['lastPulledAt' => null])->assertOk()->json('family_name'); @@ -38,17 +60,19 @@ private function pulledName(): string public function test_setup_stores_the_given_name_or_suggests_the_admins_full_name(): void { - // The field is optional: only the other fields are `required`. - $form = $this->get('/setup')->assertOk()->assertSee('name="family_name"', false)->getContent(); - $this->assertSame(1, preg_match('/]*>/', $form, $input)); + // Friendly name is optional: only the other fields are `required`. + $form = $this->get('/setup')->assertOk()->assertSee('name="friendly_name"', false)->getContent(); + $this->assertSame(1, preg_match('/]*>/', $form, $input)); $this->assertStringNotContainsString('required', $input[0]); - $this->assertSame(5, substr_count($form, ' required')); + $this->assertSame(6, substr_count($form, ' required')); + $this->assertStringContainsString("getElementById('friendly_name')", $form); - $this->post('/setup', self::FORM + ['family_name' => 'The Smiths'])->assertOk()->assertSee('The Smiths is set up'); + $this->post('/setup', self::FORM + ['friendly_name' => 'The Smiths'])->assertOk()->assertSee('The Smiths is set up'); $this->assertSame('The Smiths', app(FamilyService::class)->name()); + $this->assertSame('The Smiths', $this->actingAsAdminAndPull()); User::query()->delete(); - $this->post('/setup', self::FORM + ['family_name' => ''])->assertOk()->assertSee('Anna Example Family is set up'); + $this->post('/setup', self::FORM + ['friendly_name' => ''])->assertOk()->assertSee('Anna Example Family is set up'); // Stored at setup: a later change of the admin's own name does not rename the family. User::first()->update(['name' => 'Anna Other']); diff --git a/kinlore-api/tests/Feature/WebSetupTest.php b/kinlore-api/tests/Feature/WebSetupTest.php index 4d9117d..5177e15 100644 --- a/kinlore-api/tests/Feature/WebSetupTest.php +++ b/kinlore-api/tests/Feature/WebSetupTest.php @@ -20,7 +20,8 @@ class WebSetupTest extends TestCase use RefreshDatabase; private const FORM = [ - 'name' => 'Owner', + 'first_name' => 'Owner', + 'last_name' => 'Example', 'email' => 'owner@example.test', 'password' => 'correct horse 42', 'password_confirmation' => 'correct horse 42', @@ -205,7 +206,8 @@ public function test_pages_escape_the_instance_name(): void $this->get('/setup')->assertOk() ->assertSee('<script>alert(1)</script>', false) - ->assertDontSee('