trypost/app/Http/Middleware/HandleInertiaRequests.php

75 lines
2 KiB
PHP
Raw Normal View History

2026-01-15 01:13:44 +00:00
<?php
namespace App\Http\Middleware;
use App\Models\Language;
2026-01-15 01:13:44 +00:00
use Illuminate\Http\Request;
2026-01-21 22:35:49 +00:00
use Illuminate\Support\Facades\App;
2026-01-15 01:13:44 +00:00
use Inertia\Middleware;
class HandleInertiaRequests extends Middleware
{
/**
* The root template that's loaded on the first page visit.
*
* @see https://inertiajs.com/server-side-setup#root-template
*
* @var string
*/
protected $rootView = 'app';
/**
* Determines the current asset version.
*
* @see https://inertiajs.com/asset-versioning
*/
public function version(Request $request): ?string
{
return parent::version($request);
}
/**
* Define the props that are shared by default.
*
* @see https://inertiajs.com/shared-data
*
* @return array<string, mixed>
*/
public function share(Request $request): array
{
$user = $request->user();
2026-01-21 22:35:49 +00:00
if ($user) {
App::setLocale($user->language->code);
}
2026-01-20 20:49:16 +00:00
$currentWorkspace = $user?->currentWorkspace;
$currentRole = null;
if ($user && $currentWorkspace) {
$currentRole = $user->workspaces()
->where('workspaces.id', $currentWorkspace->id)
->first()
?->pivot
?->role;
}
2026-01-15 01:13:44 +00:00
return [
...parent::share($request),
'name' => config('app.name'),
'auth' => [
'user' => $user,
2026-01-20 20:49:16 +00:00
'role' => $currentRole,
2026-01-15 01:13:44 +00:00
],
2026-01-20 20:49:16 +00:00
'currentWorkspace' => $currentWorkspace,
'workspaces' => $user ? $user->workspaces()->select('workspaces.id', 'workspaces.name')->get() : [],
2026-01-15 01:13:44 +00:00
'sidebarOpen' => ! $request->hasCookie('sidebar_state') || $request->cookie('sidebar_state') === 'true',
2026-01-15 17:24:39 +00:00
'flash' => $request->session()->get('flash', []),
'env' => config('app.env'),
'locale' => app()->getLocale(),
'selfHosted' => config('trypost.self_hosted'),
'languages' => Language::all(),
2026-01-15 01:13:44 +00:00
];
}
}