feat(auth): self-hosted registration gate + admin seeder (closes #46)

Self-hosted installs (SELF_HOSTED=true, the default) now close /register
to the public. Workspace invites still work — the AcceptInvite page
links into /register with ?invite={id}, the middleware persists that
into the session, and POST /register passes through.

- EnsureRegistrationEnabled middleware gates GET/POST /register.
  Accepts ?invite=… (URL) or pending_invite_id (session) as the pass.
- RegisteredUserController::store clears the marker after signup.
- AcceptInvite.vue passes invite.id in the register link's query string.
- Login.vue hides the "Sign up" link when self_hosted.
- UserSeeder bootstraps a single admin (admin@trypost.it / password).
  Idempotent; not wired into DatabaseSeeder — operator runs
  `php artisan db:seed --class=UserSeeder` per the install docs.
- Tests cover both flag values for every changed surface.

Docs PR: see trypost-docs self-hosting/installation.mdx step 3.
This commit is contained in:
Paulo Castellano 2026-05-19 11:45:16 -03:00
parent e2caefec6e
commit 74bd88d3d6
12 changed files with 206 additions and 6 deletions

View file

@ -57,6 +57,8 @@ public function store(Request $request): RedirectResponse
Auth::login($user);
$request->session()->forget('pending_invite_id');
if ($redirect = $request->input('redirect')) {
if (str_starts_with($redirect, '/') && ! str_starts_with($redirect, '//')) {
return redirect($redirect);

View file

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace App\Http\Middleware\App;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class EnsureRegistrationEnabled
{
public function handle(Request $request, Closure $next): mixed
{
if (! config('trypost.self_hosted')) {
return $next($request);
}
if ($inviteId = $request->query('invite') ?? $request->session()->get('pending_invite_id')) {
$request->session()->put('pending_invite_id', $inviteId);
return $next($request);
}
throw new NotFoundHttpException;
}
}

View file

@ -1,6 +1,7 @@
<?php
use App\Http\Middleware\Api\LoadWorkspaceFromToken;
use App\Http\Middleware\App\EnsureRegistrationEnabled;
use App\Http\Middleware\App\HandleInertiaRequests;
use App\Http\Middleware\App\SetLocale;
use Illuminate\Foundation\Application;
@ -32,6 +33,7 @@
$middleware->alias([
'workspace.token' => LoadWorkspaceFromToken::class,
'registration.enabled' => EnsureRegistrationEnabled::class,
]);
$middleware->preventRequestForgery(except: [

View file

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Database\Seeders;
use App\Actions\User\CreateUser;
use App\Actions\Workspace\CreateWorkspace;
use App\Models\User;
use Illuminate\Database\Seeder;
class UserSeeder extends Seeder
{
public function run(): void
{
if (User::query()->exists()) {
$this->command->info('UserSeeder: a user already exists, skipping.');
return;
}
$user = CreateUser::execute([
'name' => 'Admin',
'email' => 'admin@trypost.it',
'password' => 'password',
'email_verified_at' => now(),
'timezone' => 'UTC',
]);
CreateWorkspace::execute($user, ['name' => 'My Workspace']);
$this->command->info('Admin account created — change the password on first login:');
$this->command->line(' email: admin@trypost.it');
$this->command->line(' password: password');
}
}

View file

@ -96,7 +96,7 @@ const inviteUrl = computed(() => `/invites/${props.invite.id}`);
</Link>
</Button>
<Button as-child variant="outline" size="lg" class="w-full">
<Link :href="register({ query: { redirect: inviteUrl, email: invite.email } })">
<Link :href="register({ query: { redirect: inviteUrl, email: invite.email, invite: invite.id } })">
{{ $t('auth.accept_invite.create_account') }}
</Link>
</Button>

View file

@ -1,5 +1,6 @@
<script setup lang="ts">
import { Form, Head } from '@inertiajs/vue3';
import { Form, Head, usePage } from '@inertiajs/vue3';
import { computed } from 'vue';
import SocialLogin from '@/components/auth/SocialLogin.vue';
import InputError from '@/components/InputError.vue';
@ -19,6 +20,9 @@ defineProps<{
email?: string | null;
redirect?: string | null;
}>();
const page = usePage();
const isSelfHosted = computed(() => Boolean(page.props.selfHosted));
</script>
<template>
@ -69,7 +73,7 @@ defineProps<{
</Button>
</div>
<div class="text-center text-sm text-muted-foreground">
<div v-if="!isSelfHosted" class="text-center text-sm text-muted-foreground">
{{ $t('auth.login.no_account') }}
<TextLink :href="register()" :tabindex="5">{{ $t('auth.login.sign_up') }}</TextLink>
</div>

View file

@ -18,8 +18,10 @@
Route::get('/invites/{invite}', [AcceptInviteController::class, 'show'])->name('app.invites.show');
Route::middleware(['guest'])->group(function () {
Route::get('/register', [RegisteredUserController::class, 'create'])->name('register');
Route::post('/register', [RegisteredUserController::class, 'store'])->name('register.store');
Route::middleware('registration.enabled')->group(function () {
Route::get('/register', [RegisteredUserController::class, 'create'])->name('register');
Route::post('/register', [RegisteredUserController::class, 'store'])->name('register.store');
});
Route::get('/login', [AuthenticatedSessionController::class, 'create'])->name('login');
Route::post('/login', [AuthenticatedSessionController::class, 'store'])->name('login.store');

View file

@ -19,7 +19,9 @@
]);
});
test('show invite displays invite details for guest', function () {
test('show invite displays invite details for guest when not self_hosted', function () {
config()->set('trypost.self_hosted', false);
$invite = Invite::factory()->create([
'account_id' => $this->account->id,
'invited_by' => $this->owner->id,
@ -39,6 +41,26 @@
);
});
test('show invite displays invite details for guest when self_hosted (page renders, gate happens on /register)', function () {
config()->set('trypost.self_hosted', true);
$invite = Invite::factory()->create([
'account_id' => $this->account->id,
'invited_by' => $this->owner->id,
'email' => 'newuser@example.com',
'workspaces' => [$this->workspace->id],
]);
$response = $this->get(route('app.invites.show', $invite));
$response->assertOk();
$response->assertInertia(fn ($page) => $page
->component('auth/AcceptInvite', false)
->has('invite')
->where('invite.id', $invite->id)
);
});
test('show invite displays invite details for authenticated user', function () {
$user = User::factory()->create([
'email' => 'invitee@example.com',

View file

@ -4,6 +4,8 @@
use App\Models\User;
beforeEach(fn () => config()->set('trypost.self_hosted', false));
test('registration screen can be rendered', function () {
$response = $this->get(route('register'));
@ -111,3 +113,74 @@
expect($user->email_verified_at)->not->toBeNull();
});
test('register page returns 404 when self_hosted and no pending invite in session', function () {
config()->set('trypost.self_hosted', true);
$response = $this->get(route('register'));
$response->assertNotFound();
});
test('register POST returns 404 when self_hosted and no pending invite in session', function () {
config()->set('trypost.self_hosted', true);
$response = $this->post(route('register.store'), [
'name' => 'Test User',
'email' => 'test@example.com',
'password' => 'Password123!',
]);
$response->assertNotFound();
expect(User::where('email', 'test@example.com')->exists())->toBeFalse();
});
test('register page renders when self_hosted but session has pending invite', function () {
config()->set('trypost.self_hosted', true);
$response = $this
->withSession(['pending_invite_id' => 'invite-abc'])
->get(route('register'));
$response->assertOk();
});
test('register page renders when self_hosted with invite query param and persists it to session', function () {
config()->set('trypost.self_hosted', true);
$response = $this->get(route('register', ['invite' => 'invite-xyz']));
$response->assertOk();
$response->assertSessionHas('pending_invite_id', 'invite-xyz');
});
test('signup clears pending_invite_id from session', function () {
config()->set('trypost.self_hosted', true);
$this->withSession(['pending_invite_id' => 'invite-abc'])
->post(route('register.store'), [
'name' => 'Invitee',
'email' => 'invitee@example.com',
'password' => 'Password123!',
]);
expect(session('pending_invite_id'))->toBeNull();
expect(User::where('email', 'invitee@example.com')->exists())->toBeTrue();
});
test('register works normally when not self_hosted even with pending invite in session', function () {
config()->set('trypost.self_hosted', false);
$response = $this
->withSession(['pending_invite_id' => 'invite-abc'])
->post(route('register.store'), [
'name' => 'Invitee',
'email' => 'invitee@example.com',
'password' => 'Password123!',
]);
$response->assertSessionHasNoErrors();
expect(User::where('email', 'invitee@example.com')->exists())->toBeTrue();
// Cleared regardless of mode, since signup consumes the marker.
expect(session('pending_invite_id'))->toBeNull();
});

View file

@ -6,6 +6,8 @@
use Laravel\Socialite\Facades\Socialite;
use Laravel\Socialite\Two\User as SocialiteUser;
beforeEach(fn () => config()->set('trypost.self_hosted', false));
test('email registration saves utm parameters from the register page query string', function () {
$utms = [
'utm_source' => 'peerlist',

View file

@ -2,6 +2,8 @@
declare(strict_types=1);
beforeEach(fn () => config()->set('trypost.self_hosted', false));
test('login page shares github auth enabled prop as false when disabled', function () {
config(['trypost.github_auth_enabled' => false]);
@ -52,3 +54,16 @@
// Should redirect to login on failure (no OAuth code), not 404
$response->assertRedirect(route('login'));
});
test('register page still shares github auth enabled prop when self_hosted (via pending invite)', function () {
config()->set('trypost.self_hosted', true);
config()->set('trypost.github_auth_enabled', true);
$response = $this
->withSession(['pending_invite_id' => 'invite-abc'])
->get(route('register'));
$response->assertOk();
$page = $response->original->getData()['page'];
expect($page['props']['githubAuthEnabled'])->toBeTrue();
});

View file

@ -2,6 +2,8 @@
declare(strict_types=1);
beforeEach(fn () => config()->set('trypost.self_hosted', false));
test('login page loads when google auth is disabled', function () {
config(['trypost.google_auth_enabled' => false]);
@ -69,3 +71,16 @@
// Should redirect to login on failure (no OAuth code), not 404
$response->assertRedirect(route('login'));
});
test('register page still shares google auth enabled prop when self_hosted (via pending invite)', function () {
config()->set('trypost.self_hosted', true);
config()->set('trypost.google_auth_enabled', true);
$response = $this
->withSession(['pending_invite_id' => 'invite-abc'])
->get(route('register'));
$response->assertOk();
$page = $response->original->getData()['page'];
expect($page['props']['googleAuthEnabled'])->toBeTrue();
});