78 lines
1.7 KiB
PHP
78 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Inertia\Middleware;
|
|
use Laravel\Nova\Nova;
|
|
|
|
final class HandleInertiaRequests extends Middleware
|
|
{
|
|
/**
|
|
* The root template that is loaded on the first page visit.
|
|
*/
|
|
protected $rootView = 'app';
|
|
|
|
/**
|
|
* Determine the current asset version.
|
|
*/
|
|
public function version(Request $request): ?string
|
|
{
|
|
return parent::version($request);
|
|
}
|
|
|
|
/**
|
|
* Define the props that are shared by default.
|
|
*/
|
|
public function share(Request $request): array
|
|
{
|
|
return [
|
|
...parent::share($request),
|
|
'auth' => [
|
|
'user' => $this->getAuthenticatedUser(),
|
|
'logo_href' => $this->getLogoHref(),
|
|
],
|
|
'flash' => [
|
|
'success' => fn () => Arr::get($request->session()->all(), 'success'),
|
|
'error' => fn () => Arr::get($request->session()->all(), 'error'),
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get authenticated user data for frontend.
|
|
*/
|
|
private function getAuthenticatedUser(): ?array
|
|
{
|
|
$user = auth()->user();
|
|
|
|
if ($user === null) {
|
|
return null;
|
|
}
|
|
|
|
return [
|
|
'id' => $user->id,
|
|
'name' => $user->name,
|
|
'email' => $user->email,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Determine logo href based on user Nova access.
|
|
*/
|
|
private function getLogoHref(): string
|
|
{
|
|
$user = auth()->user();
|
|
|
|
if ($user !== null && Gate::allows('viewNova', $user)) {
|
|
return Nova::path();
|
|
}
|
|
|
|
return '/';
|
|
}
|
|
}
|