I need multiple guard logins like Seller, Customer, and Admin
Do you really need custom guards; or do you need to implement Authorization correctly?
I am developing an e-commerce website. Currently, I am using Laravel Livewire for this project. I need multiple guard logins like Seller, Customer, and Admin. How can I create a custom guard in Laravel Livewire?
I need multiple guard logins like Seller, Customer, and Admin
Do you really need custom guards; or do you need to implement Authorization correctly?
@tykus Yes! I need a custom guard authentication. Because I am developing separate dashboards for users, sellers and admins.
@LearnWithMoin no, you do not. Separate dashboards / pages can be achieved using Role Based Access Control (RBAC)
@tykus But I need a separate login for this website.
@LearnWithMoin do you really? You can have different login views that all POST to the same endpoint and handle post-login redirection based on the authenticated user's role?
Anyway, you have been told from the outset by myself and @martinbean that custom guards are not needed to achieve what you need, but you choose not to heed the advice, so good luck with your project.
I need multiple guard logins like Seller, Customer, and Admin. How can I create a custom guard in Laravel Livewire?
@learnwithmoin Why? Users are users.
As @tykus says, use roles and authorisation to determine what a user can and cannot do based on their role(s).
https://martinbean.dev/blog/2021/07/29/simple-role-based-authentication-laravel/
@martinbean Ok. But I need multiple custom guards for a separate dashboard and database query.
@LearnWithMoin You don't need multiple guards. You can create gates for authorization. For example:
Gate::define('admin', function(User $user) {
return $user->role === 'admin';
});
You can then use them as middleware:
Route::middleware('can:admin')->group(function() {
Route::post('/admin/do-stuff/', [AdminController::class, 'doStuff']);
Route::post('/admin/other-stuff/', [AdminController::class, 'otherStuff']);
});
Or you can use them in code:
if($request->user()->can('admin')) {
...
}
I need multiple custom guards for a separate dashboard
@LearnWithMoin No, you don’t. You just need to use authorisation to determine what routes a user can and cannot access based on their role(s).
Route::middleware('auth')->group(function () {
Route::middleware('role:seller')->group(function () {
// Seller-only routes...
});
Route::middleware('role:admin')->group(function () {
// Admin-only routes...
});
});
dont do it. Everyone shoukd be authenticated using the same mechanism. You can then use whatever method you like to decide where to send them and control what they can do.
Please or to participate in this conversation.