trypost/app/Http/Middleware/EnsureSubscribed.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

48 lines
1.3 KiB
PHP

<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class EnsureSubscribed
{
/**
* Handle an incoming request.
*
* @param Closure(Request): (Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
// Skip subscription check for self-hosted mode
if (config('trypost.self_hosted')) {
return $next($request);
}
$user = $request->user();
if (! $user) {
return redirect()->route('login');
}
// Allow access if user has active subscription or is on trial
if ($user->subscribed('default') || $user->onTrial('default')) {
return $next($request);
}
// Allow access if user belongs to a workspace owned by a subscribed user
$currentWorkspace = $user->currentWorkspace;
if ($currentWorkspace && $currentWorkspace->owner && $currentWorkspace->owner->id !== $user->id) {
$owner = $currentWorkspace->owner;
if ($owner->subscribed('default') || $owner->onTrial('default')) {
return $next($request);
}
}
// Redirect to subscription page
return redirect()->route('app.subscribe');
}
}