I've been trying to figure out a way to make 3 types of user access to pages:
guest
admin
user
For example:
/homepage
and /login
/homepage
, /logout
and /manage
/homepage
, /logout
and /shopping/cart
Guest
is not signed in. Admin
and User
share some routes, but not all routes should be equal access between the 2
I've been using a workaround in the views to show 3 separate displays:
@auth
// Paragraph seen by the user with name `admin`
@if(auth()->user()->attribute == 'admin')
<p>ADMIN</p>
// Paragraph seen by every other user
@else
<p>USER</p>
@endif
// Paragraph seen by guests
@else
<p>GUEST</p>
@endauth
This serves the purpose of hiding things on the page using the attribute
value of the user, but I want a way to apply the 3 access tiers on entire routes since regular users can get access to management pages.
you can handle it with guards instead. in config/auth.php define your desired guards like below:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
'adminapi' => [
'driver' => 'passport',
'provider' => 'admins',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
],
the define the providers:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => Modules\User\Entities\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => \Modules\Operator\Entities\Operator::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
after that you can use it in your code like this:
auth()->guard('admin') ? print('admin') : print('user')
or in your blades with with @auth directive
you can get more infohere