trypost/app/Http/Middleware/App/EnsureAccountReady.php
Paulo Castellano dba6346226 refactor(auth): split workspace gate into EnsureHasWorkspace middleware
EnsureAccountReady bundled a subscription gate (redirects to onboarding,
SaaS only) with a workspace gate (redirects to workspace creation). The
connect routes can't sit behind it because connecting/disconnecting
happens during onboarding, before a subscription exists.

Split the workspace gate into a standalone EnsureHasWorkspace middleware:

- EnsureAccountReady is now subscription-only.
- EnsureHasWorkspace redirects to workspace creation when there is no
  current workspace, in both SaaS and self-hosted modes.
- The social connect group gains EnsureHasWorkspace; the main app group
  gains it alongside EnsureAccountReady (listed after it, so the
  subscription gate still runs first — no custom middleware priority).
- The repeated `if (! $workspace) redirect()` guard is removed from the
  connect/store/authorize/disconnect/index/toggle handlers, and their
  return types are tightened (no more dangling RedirectResponse).

LinkedIn connect's no-workspace path changes from a popup callback to the
same redirect as the other platforms.
2026-06-25 14:30:15 -03:00

40 lines
990 B
PHP

<?php
declare(strict_types=1);
namespace App\Http\Middleware\App;
use App\Models\Account;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class EnsureAccountReady
{
/**
* @param Closure(Request): (Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
$user = $request->user();
if (! $user) {
return $next($request);
}
if (! config('trypost.self_hosted')) {
$account = $user->account;
$requiresCardForTrial = (bool) config('trypost.billing.require_card_for_trial', true);
$hasAccess = $account && (
$account->subscribed(Account::SUBSCRIPTION_NAME)
|| (! $requiresCardForTrial && $account->isOnTrial())
);
if (! $hasAccess) {
return redirect()->route('app.onboarding');
}
}
return $next($request);
}
}