Hey guys, in this article, am going to show you how toimplement multiple role-based authentication in Laravel even if you have many different users...
For further actions, you may consider blocking this person and/or reporting abuse
And here's another refactor to make it even conciser and easier to read and change. It is using a lookup array to isolate changes (just add a new item to the array for a new role) and have one execution path (one
redirect
line instead of 5).I'm Having errors after implementing this.
I logged in as
academy
then try to visit theadmin
dashboard by typing the route to it in the browser and i got this ->"Call to undefined method Illuminate\Auth\AuthManager::user()"
In Martin's implementation there's a typo of the parenthesis when calling the Auth facade... So instead of
Auth()::user()->role
, it should beAuth::user()->role
Oh that's true.. I will fix that..
I'll replicate it soon, probably I just have a typo somewhere.
Sure. I will be expecting.
a comment that make me awkwardly happy
Wow.. it's getting more clean and clear.. Thanks for the update.
May I propose a refactor for the
handle
method? You could save some duplications by flipping the logic. If you first ask forif the user is not logged in, redirect to login
you catch this case once and can skip it for everything that follows. Also, instead ofelse
…else
, you can simply useif
…if
. If a case proves true, you return something and the function stops, so you do not need anelse
. This reduces cognitive load.What about switch statement?
Yes,
switch
would cut some duplicate lines. I would still propose the lookup array pattern as recommended in this extra comment as it is a lot easier to add and cut arguments and is super easy to read.These if statements could also be one-liners to save some more characters and space.
Yes, but there still would be lots of repetitions even in short-form. Maybe I can write an own article soon about possible refactorings so that people can learn and compare. The original poster went with the simple
if
solution but there are multiple ways to make it shorter and easier to maintain.Indeed.
Tell me where can I start a new discussion?
Just put a comment on the root of this article: dev.to/kaperskyguru/multiple-role-...
This is great, thanks for your contribution, I will refactor immediately.
Updated now..
It was a beautiful tutorial, there is a routing refactor must be done tho, in order to function properly in laravel 8
Route::get('/player', [PlayerController::class,'index'])->name('player')->middleware('player');
instead of
Route::get('/player', 'PlayerController@index')->name('player')->middleware('player');
there are other ways to solve this problem but that just my favorite one
This work for page level and route level access control. Is there any guide for page element access control? Such as hide delete button for non-admin, disabled edit capability for certain form field access control?
You can use action based control, like Spartia library for such.
github.com/spatie/laravel-permission
created role middleware but getting an error
error: Attempt to read property "role" on null
middleware function below as :
public function handle(Request $request, Closure $next)
{
if (!Auth::check()) {
i used your code and i am getting this error
127.0.0.1 redirected you too many times.
How if the user have multiple account with different roles,but with the same credentials ? I assumed this nice guide not covered it yet :)
In that case, create a separate table for roles and permissions (spatie/laravel-permission can help). Then, create the permissions and roles and assign permissions to roles. I would recommend as best practice to rely mainly on permissions in your applications as differents roles may have common permissions. Now, when a user is created assign him a role, and rely on this first role for redirect ($user->roles()->id). You can have a menu where he can switch to another dashboard based on his roles. Hope it helps.
Yes the article didn't cover that. But I will suggest you use OTP to implement such scenerios.
Great!
I wish to know how to handle when a user has multiple roles ie. superadmin and admin
Nice, this was a great tutorial!