From fcdcb089cc86ac24d06cfa8635671619d9155fc0 Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Fri, 30 Oct 2026 11:13:57 +0100 Subject: [PATCH] feat: registration checks name rules and uniqueness, email, password and accepted terms, and names the wrong field; players get protected_until and last_active_at Co-Authored-By: Claude Opus 5.5 (1M context) --- app/Http/Controllers/AuthController.php | 44 ++++++++++++------- app/Models/User.php | 3 +- .../2026_11_01_090000_launch_accounts.php | 31 +++++++++++++ tests/Feature/AuthTest.php | 22 ++++++++-- 4 files changed, 79 insertions(+), 21 deletions(-) create mode 100644 database/migrations/2026_11_01_090000_launch_accounts.php diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index 7b865f0..f4a6a90 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -10,28 +10,40 @@ use Illuminate\Support\Facades\Hash; /** Issues the bearer token the WS client sends when it subscribes. Everything else is WS. */ class AuthController extends Controller { + /** 3 to 24 characters: letters, digits, and spaces, dots, dashes or underscores inside. */ + public const NAME = '/^[\p{L}\p{N}][\p{L}\p{N} ._\-]{1,22}[\p{L}\p{N}]$/u'; + + /** + * A new account. Every refusal names the field and a code the client translates, so + * the form can mark the right field. + */ public function register(Request $request): JsonResponse { - $data = $request->validate([ - 'name' => ['required', 'string', 'max:40'], - 'email' => ['required', 'email', 'max:255'], - 'password' => ['required', 'string', 'min:8', 'max:255'], - ]); - if (User::where('email', $data['email'])->exists()) { - return $this->refuse('email_taken', 'This email is already registered.'); + $name = trim((string) $request->input('name')); + $email = mb_strtolower(trim((string) $request->input('email'))); + $password = (string) $request->input('password'); + + $refusal = match (true) { + ! preg_match(self::NAME, $name) || str_contains($name, ' ') => ['name', 'name_invalid', 'Names are 3 to 24 letters, digits, spaces, dots, dashes or underscores.'], + User::whereRaw('LOWER(name) = ?', [mb_strtolower($name)])->exists() => ['name', 'name_taken', 'That name is taken.'], + ! filter_var($email, FILTER_VALIDATE_EMAIL) || mb_strlen($email) > 255 => ['email', 'email_invalid', 'That is not an email address.'], + User::where('email', $email)->exists() => ['email', 'email_taken', 'This email is already registered.'], + mb_strlen($password) < 8 || mb_strlen($password) > 255 => ['password', 'password_short', 'Passwords are at least 8 characters.'], + ! $request->boolean('terms') => ['terms', 'terms_required', 'Accept the terms to play.'], + default => null, + }; + if ($refusal) { + return $this->refuse($refusal[1], $refusal[2], $refusal[0]); } - return $this->issue(User::create($data)); + return $this->issue(User::create(['name' => $name, 'email' => $email, 'password' => $password, 'terms_accepted_at' => now()])); } public function login(Request $request): JsonResponse { - $data = $request->validate([ - 'email' => ['required', 'email'], - 'password' => ['required', 'string'], - ]); - $user = User::where('email', $data['email'])->first(); - if (! $user || ! Hash::check($data['password'], $user->password)) { + $email = mb_strtolower(trim((string) $request->input('email'))); + $user = User::where('email', $email)->first(); + if (! $user || ! Hash::check((string) $request->input('password'), $user->password)) { return $this->refuse('invalid_credentials', 'Wrong email or password.'); } @@ -46,8 +58,8 @@ class AuthController extends Controller ]); } - private function refuse(string $code, string $message): JsonResponse + private function refuse(string $code, string $message, ?string $field = null): JsonResponse { - return response()->json(['code' => $code, 'message' => $message], 422); + return response()->json(['code' => $code, 'message' => $message, 'field' => $field], 422); } } diff --git a/app/Models/User.php b/app/Models/User.php index a06896d..535d95d 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -12,7 +12,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; -#[Fillable(['name', 'email', 'password'])] +#[Fillable(['name', 'email', 'password', 'terms_accepted_at'])] #[Hidden(['password', 'remember_token'])] class User extends Authenticatable { @@ -34,6 +34,7 @@ class User extends Authenticatable { return [ 'email_verified_at' => 'datetime', + 'terms_accepted_at' => 'datetime', 'password' => 'hashed', ]; } diff --git a/database/migrations/2026_11_01_090000_launch_accounts.php b/database/migrations/2026_11_01_090000_launch_accounts.php new file mode 100644 index 0000000..d04f55e --- /dev/null +++ b/database/migrations/2026_11_01_090000_launch_accounts.php @@ -0,0 +1,31 @@ +timestamp('terms_accepted_at')->nullable(); + }); + Schema::table('players', function (Blueprint $table) { + // Until when nobody may attack them: from joining, and again from a restart. + $table->dateTime('protected_until', 3)->nullable(); + $table->dateTime('last_active_at', 3)->nullable()->index(); + }); + } + + public function down(): void + { + Schema::table('players', function (Blueprint $table) { + $table->dropColumn(['protected_until', 'last_active_at']); + }); + Schema::table('users', function (Blueprint $table) { + $table->dropColumn('terms_accepted_at'); + }); + } +}; diff --git a/tests/Feature/AuthTest.php b/tests/Feature/AuthTest.php index b9e7d91..7bfc43a 100644 --- a/tests/Feature/AuthTest.php +++ b/tests/Feature/AuthTest.php @@ -4,18 +4,19 @@ use App\Models\User; it('registers a player and returns a bearer token', function () { $response = $this->postJson('/api/auth/register', [ - 'name' => 'Ada', 'email' => 'ada@example.test', 'password' => 'secret-pass', + 'name' => 'Ada', 'email' => 'Ada@Example.test', 'password' => 'secret-pass', 'terms' => true, ]); $response->assertOk()->assertJsonPath('user.name', 'Ada')->assertJsonStructure(['token', 'user' => ['id', 'name', 'email']]); - expect(User::where('email', 'ada@example.test')->first()->tokens)->toHaveCount(1); + $user = User::where('email', 'ada@example.test')->first(); + expect($user->tokens)->toHaveCount(1)->and($user->terms_accepted_at)->not->toBeNull(); }); it('refuses a second account with the same email', function () { User::factory()->create(['email' => 'ada@example.test']); - $this->postJson('/api/auth/register', ['name' => 'Ada', 'email' => 'ada@example.test', 'password' => 'secret-pass']) - ->assertStatus(422)->assertJsonPath('code', 'email_taken'); + $this->postJson('/api/auth/register', ['name' => 'Ada', 'email' => 'ada@example.test', 'password' => 'secret-pass', 'terms' => true]) + ->assertStatus(422)->assertJsonPath('code', 'email_taken')->assertJsonPath('field', 'email'); }); it('logs in with the right password only', function () { @@ -26,3 +27,16 @@ it('logs in with the right password only', function () { $this->postJson('/api/auth/login', ['email' => 'ada@example.test', 'password' => 'secret-pass']) ->assertOk()->assertJsonStructure(['token']); }); + +it('checks every field and names the one that is wrong', function () { + User::factory()->create(['name' => 'Taken One']); + $try = fn (array $over) => $this->postJson('/api/auth/register', [...['name' => 'Ada', 'email' => 'ada@example.test', 'password' => 'secret-pass', 'terms' => true], ...$over]); + + $try(['name' => 'A'])->assertStatus(422)->assertJsonPath('code', 'name_invalid')->assertJsonPath('field', 'name'); + $try(['name' => '