Add authentication, a public landing page, and guest data preview

Hand-rolled login/register/logout on Laravel's built-in Auth facade
rather than Breeze/Jetstream, which scaffold Tailwind v3 config and
would fight our existing Tailwind v4 CSS-first setup. Login/Register
are Livewire components; logout is a plain POST route (auth-gated,
CSRF-protected).

Routing: "/" is now a public landing page describing the project;
the sites list moved from "/" to "/sites" to make room for it.

Data gating: App\Support\GuestPreview::paginate() forces page 1 and a
5-row page size for unauthenticated visitors on Sites, Discover, and
a site's Pages/Backlinks tables, while total() still reflects the
real match count — authenticated users get the existing full
pagination unchanged. A shared guest-preview-notice component shows
"N of M results, sign in to see all" and only renders when something
is actually being hidden.

Testing: pointed the test suite at a dedicated MySQL database
(huginn_testing) instead of the default in-memory SQLite, since the
existing fulltext index migration isn't SQLite-compatible — this app
is MySQL-specific by now regardless. Added AuthFlowTest (register,
login, wrong-password rejection, logout) and GuestPreviewTest (guest
capped to a preview, authenticated sees everything), all passing.

Also fixed a real bug the tests caught: request()->session() isn't
reliably attached during Livewire's in-process test action calls;
switched Login/Register to the session() helper, which resolves the
session store directly and works in both contexts.
This commit is contained in:
m105
2026-07-25 15:22:42 +03:00
parent 4c0cc5a7f5
commit f5c9129774
18 changed files with 461 additions and 25 deletions
+42
View File
@@ -0,0 +1,42 @@
<?php
namespace App\Livewire\Auth;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;
use Livewire\Attributes\Layout;
use Livewire\Component;
#[Layout('components.layouts.app')]
class Login extends Component
{
public string $email = '';
public string $password = '';
public bool $remember = false;
public function login(): void
{
$this->validate([
'email' => ['required', 'email'],
'password' => ['required'],
]);
if (! Auth::attempt(['email' => $this->email, 'password' => $this->password], $this->remember)) {
throw ValidationException::withMessages([
'email' => 'These credentials do not match our records.',
]);
}
session()->regenerate();
$this->redirect(route('sites.index'), navigate: false);
}
public function render(): View
{
return view('livewire.auth.login');
}
}
+48
View File
@@ -0,0 +1,48 @@
<?php
namespace App\Livewire\Auth;
use App\Models\User;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rules\Password;
use Livewire\Attributes\Layout;
use Livewire\Component;
#[Layout('components.layouts.app')]
class Register extends Component
{
public string $name = '';
public string $email = '';
public string $password = '';
public string $password_confirmation = '';
public function register(): void
{
$validated = $this->validate([
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'email', 'max:255', 'unique:users,email'],
'password' => ['required', 'confirmed', Password::defaults()],
]);
$user = User::create([
'name' => $validated['name'],
'email' => $validated['email'],
'password' => $validated['password'],
]);
Auth::login($user);
session()->regenerate();
$this->redirect(route('sites.index'), navigate: false);
}
public function render(): View
{
return view('livewire.auth.register');
}
}
+3 -3
View File
@@ -3,6 +3,7 @@
namespace App\Livewire\Discover;
use App\Models\Site;
use App\Support\GuestPreview;
use Illuminate\Contracts\View\View;
use Illuminate\Pagination\LengthAwarePaginator;
use Livewire\Attributes\Layout;
@@ -67,7 +68,7 @@ class Search extends Component
if (! $hasQuery) {
$sites = new LengthAwarePaginator([], 0, 25, 1, ['path' => request()->url()]);
} else {
$sites = Site::query()
$sites = GuestPreview::paginate(Site::query()
->when(
$keyword !== '',
fn ($query) => $query->whereHas(
@@ -98,8 +99,7 @@ class Search extends Component
}
}])
->orderByDesc('matching_pages_count')
->orderBy('domain')
->paginate(25);
->orderBy('domain'));
}
return view('livewire.discover.search', ['sites' => $sites, 'hasQuery' => $hasQuery]);
+10 -8
View File
@@ -3,6 +3,7 @@
namespace App\Livewire\Sites;
use App\Models\Site;
use App\Support\GuestPreview;
use Illuminate\Contracts\View\View;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Url;
@@ -24,14 +25,15 @@ class Index extends Component
public function render(): View
{
$sites = Site::query()
->withCount('pages')
->when(
$this->search !== '',
fn ($query) => $query->where('domain', 'like', '%'.$this->search.'%')
)
->orderBy('domain')
->paginate(25);
$sites = GuestPreview::paginate(
Site::query()
->withCount('pages')
->when(
$this->search !== '',
fn ($query) => $query->where('domain', 'like', '%'.$this->search.'%')
)
->orderBy('domain')
);
return view('livewire.sites.index', ['sites' => $sites]);
}