trypost/app/Http/Middleware/EnsureUserSetupIsComplete.php
Paulo Castellano 8689e54e55 refactor: restructure to Actions, subdomain routes (app/api), API tokens
- Extract business logic from controllers into Action classes:
  Post/, Workspace/, Hashtag/, Label/, Invite/, ApiKey/
- Create subdomain routing: app.trypost.test (Inertia dashboard),
  api.trypost.test (REST API with token auth)
- Add ApiToken model with tp_ prefix, token_lookup/hash auth
- Add AuthenticateApiToken middleware for API authentication
- Create Api controllers with JSON Resources for all entities
- Create App controllers that use Actions + Inertia responses
- Organize Form Requests into Api/ and App/ directories
- Add api_tokens migration
- Update all route names with app. prefix
- Update all tests to use new route names (684 passing)
2026-03-29 19:24:28 -03:00

55 lines
1.7 KiB
PHP

<?php
namespace App\Http\Middleware;
use App\Enums\User\Setup;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class EnsureUserSetupIsComplete
{
/**
* Handle an incoming request.
*
* @param Closure(Request): (Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
$user = $request->user();
if (! $user) {
return $next($request);
}
// If setup is completed, allow through
if ($user->setup === Setup::Completed) {
return $next($request);
}
// Map setup status to allowed routes
$allowedRoutes = match ($user->setup) {
Setup::Role => ['onboarding.step1', 'onboarding.step1.store'],
Setup::Connections => ['onboarding.step2', 'onboarding.step2.store', 'social.*'],
Setup::Subscription => ['onboarding.complete', 'onboarding.step2'],
default => ['onboarding.step1', 'onboarding.step1.store'],
};
$currentRoute = $request->route()?->getName();
// Check if current route is allowed
foreach ($allowedRoutes as $pattern) {
if ($currentRoute === $pattern || fnmatch($pattern, $currentRoute ?? '')) {
return $next($request);
}
}
// Redirect to appropriate step
return match ($user->setup) {
Setup::Role => redirect()->route('app.onboarding.step1'),
Setup::Connections => redirect()->route('app.onboarding.step2'),
Setup::Subscription => redirect()->route('app.onboarding.step2'),
default => redirect()->route('app.onboarding.step1'),
};
}
}