From a40e7e549f5e4d108733192488dc49ad5f72670a Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Wed, 28 Oct 2026 19:17:34 +0100 Subject: [PATCH] feat: HTTP login/register and the world/village WS controllers Co-Authored-By: Claude Opus 5.5 --- app/Http/Controllers/AuthController.php | 53 +++++++++++++++ .../Controllers/VillageController.php | 67 +++++++++++++++++++ app/Websocket/Controllers/WorldController.php | 23 +++++++ app/Websocket/GameController.php | 26 +++++++ routes/api.php | 9 +++ tests/Feature/AuthTest.php | 28 ++++++++ 6 files changed, 206 insertions(+) create mode 100644 app/Http/Controllers/AuthController.php create mode 100644 app/Websocket/Controllers/VillageController.php create mode 100644 app/Websocket/Controllers/WorldController.php create mode 100644 app/Websocket/GameController.php create mode 100644 routes/api.php create mode 100644 tests/Feature/AuthTest.php diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php new file mode 100644 index 0000000..7b865f0 --- /dev/null +++ b/app/Http/Controllers/AuthController.php @@ -0,0 +1,53 @@ +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.'); + } + + return $this->issue(User::create($data)); + } + + 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)) { + return $this->refuse('invalid_credentials', 'Wrong email or password.'); + } + + return $this->issue($user); + } + + private function issue(User $user): JsonResponse + { + return response()->json([ + 'token' => $user->createToken('game')->plainTextToken, + 'user' => $user->only('id', 'name', 'email'), + ]); + } + + private function refuse(string $code, string $message): JsonResponse + { + return response()->json(['code' => $code, 'message' => $message], 422); + } +} diff --git a/app/Websocket/Controllers/VillageController.php b/app/Websocket/Controllers/VillageController.php new file mode 100644 index 0000000..65cc54f --- /dev/null +++ b/app/Websocket/Controllers/VillageController.php @@ -0,0 +1,67 @@ +village(fn ($user, $id) => app(ShowVillage::class)($user, $id)); + } + + public function build() + { + $x = request('x'); + $y = request('y'); + + return $this->village(fn ($user, $id) => app(QueueBuild::class)( + $user, + $id, + (string) request('type'), + is_numeric($x) ? (int) $x : null, + is_numeric($y) ? (int) $y : null, + )); + } + + public function upgrade() + { + return $this->village(fn ($user, $id) => app(QueueUpgrade::class)($user, $id, (int) request('building_id'))); + } + + public function demolish() + { + return $this->village(fn ($user, $id) => app(QueueDemolish::class)($user, $id, (int) request('building_id'))); + } + + public function cancel() + { + return $this->village(fn ($user, $id) => app(CancelOrder::class)($user, $id, (int) request('order_id'))); + } + + public function staff() + { + return $this->village(fn ($user, $id) => app(SetPriority::class)($user, $id, (int) request('building_id'), (int) request('priority'))); + } + + /** @param Closure(User, int): Village $action */ + private function village(Closure $action) + { + return $this->play(fn () => VillagePresenter::present( + $action(auth()->user(), (int) request('village_id')), + now()->toImmutable(), + )); + } +} diff --git a/app/Websocket/Controllers/WorldController.php b/app/Websocket/Controllers/WorldController.php new file mode 100644 index 0000000..26c7bc4 --- /dev/null +++ b/app/Websocket/Controllers/WorldController.php @@ -0,0 +1,23 @@ +play(fn () => app(ListWorlds::class)(auth()->user())); + } + + public function join() + { + return $this->play(fn () => [ + 'village_id' => app(JoinWorld::class)(auth()->user(), (int) request('world_id'))->id, + ]); + } +} diff --git a/app/Websocket/GameController.php b/app/Websocket/GameController.php new file mode 100644 index 0000000..59f19b7 --- /dev/null +++ b/app/Websocket/GameController.php @@ -0,0 +1,26 @@ +:error` with `{message, code}` so the client can translate the code. + * Lives outside Controllers/ so the resolver never maps an event to it. + */ +abstract class GameController extends Controller +{ + public $need_auth = true; + + protected function play(Closure $intent): mixed + { + try { + return $intent(); + } catch (GameRuleViolation $e) { + return $this->error(['message' => $e->getMessage(), 'code' => $e->reason]); + } + } +} diff --git a/routes/api.php b/routes/api.php new file mode 100644 index 0000000..60396e9 --- /dev/null +++ b/routes/api.php @@ -0,0 +1,9 @@ +group(function () { + Route::post('auth/register', [AuthController::class, 'register']); + Route::post('auth/login', [AuthController::class, 'login']); +}); diff --git a/tests/Feature/AuthTest.php b/tests/Feature/AuthTest.php new file mode 100644 index 0000000..b9e7d91 --- /dev/null +++ b/tests/Feature/AuthTest.php @@ -0,0 +1,28 @@ +postJson('/api/auth/register', [ + 'name' => 'Ada', 'email' => 'ada@example.test', 'password' => 'secret-pass', + ]); + + $response->assertOk()->assertJsonPath('user.name', 'Ada')->assertJsonStructure(['token', 'user' => ['id', 'name', 'email']]); + expect(User::where('email', 'ada@example.test')->first()->tokens)->toHaveCount(1); +}); + +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'); +}); + +it('logs in with the right password only', function () { + User::factory()->create(['email' => 'ada@example.test', 'password' => 'secret-pass']); + + $this->postJson('/api/auth/login', ['email' => 'ada@example.test', 'password' => 'wrong-pass']) + ->assertStatus(422)->assertJsonPath('code', 'invalid_credentials'); + $this->postJson('/api/auth/login', ['email' => 'ada@example.test', 'password' => 'secret-pass']) + ->assertOk()->assertJsonStructure(['token']); +});