Skip to content

Commit 303e0d6

Browse files
[11.x] Update authorization.md - include Vue JS approaches (laravel#9596)
* Update authorization.md - include Vue JS approaches Would be great to see the Inertia approach when using authorization in the frontend. I'm not super happy about the Warning copy, but figured this would be a good way to get the discussion happening around this * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 047eb0f commit 303e0d6

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

authorization.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- [Via Middleware](#via-middleware)
2323
- [Via Blade Templates](#via-blade-templates)
2424
- [Supplying Additional Context](#supplying-additional-context)
25+
- [Authorization & Inertia](#authorization-and-inertia)
2526

2627
<a name="introduction"></a>
2728
## Introduction
@@ -728,3 +729,45 @@ When attempting to determine if the authenticated user can update a given post,
728729

729730
return redirect('/posts');
730731
}
732+
733+
<a name="authorization-and-inertia"></a>
734+
## Authorization & Inertia
735+
736+
Although authorization must always be handled on the server, it can often be convenient to provide your frontend application with authorization data in order to properly render your application's UI. Laravel does not define a required convention for exposing authorization information to an Inertia powered frontend.
737+
738+
However, if you are using one of Laravel's Inertia-based [starter kits](/docs/{{version}}/starter-kits), your application already contains a `HandleInertiaRequests` middleware. Within this middleware's `share` method, you may return shared data that will provided to all Inertia pages in your application. This shared data can serve as a convenient location to define authorization information for the user:
739+
740+
```php
741+
<?php
742+
743+
namespace App\Http\Middleware;
744+
745+
use App\Models\Post;
746+
use Illuminate\Http\Request;
747+
use Inertia\Middleware;
748+
749+
class HandleInertiaRequests extends Middleware
750+
{
751+
// ...
752+
753+
/**
754+
* Define the props that are shared by default.
755+
*
756+
* @return array<string, mixed>
757+
*/
758+
public function share(Request $request)
759+
{
760+
return [
761+
...parent::share($request),
762+
'auth' => [
763+
'user' => $request->user(),
764+
'permissions' => [
765+
'post' => [
766+
'create' => $request->user()->can('create', Post::class),
767+
],
768+
],
769+
],
770+
];
771+
}
772+
}
773+
```

0 commit comments

Comments
 (0)