问题
I reviewed all tutorials on multiauth (without using is_admin) with different tables for different levels of authorization. I have two authorization entities: boss and admin (Manager and admin)
However, unlike all these tutorials:
- I don't have a separate authorization page. The authorization window is located on the main page and appears when you click on the authorization icon (using JS, just display:block)
- All these tutorials have separate authorization pages for different roles (separately for the Manager and separately for the admin). I have one authorization window, so I need to authenticate the user on one window (for two roles at once: admin and Manager)
- There is no use of blade directives in any tutorial (I've already looked through all sorts of tutorials). Just unique for each content I have is contained directly on the main page (I do not have a separate admin panel and personal account Manager). What should I do in this case? To write a custom Directive? Does Laravel have something ready out of the box?
Sorry for so many questions, I just have nowhere to turn, there is really nothing left on the Internet (maybe I can't find it)
The most useful thing I could find (of all the tutorials, including youtube-video tutorials of course):
- https://www.codermen.com/blog/123/how-to-make-multi-auth-in-laravel-6
- https://www.siddharthshukla.in/blog/how-to-use-multiple-authentication-guards-in-laravel-6-app/ --> I made my own authentication using this method
回答1:
You don't need separate login pages for different user roles. you need one. if you have user table like this
id type name email ...
1 admin Plamen Penchev
2 boss Ivan Ivanov
after user logged in successfully you will use this user type to load different content depends on user role Let's look for a example from one of my projects
@if (Auth::user())
<ul class="login-register profile-details">
<li>
<a href="{{route('user.profile')}}">
<img src="{{asset(Auth::user()->image)}}" />{{Auth::user()->name}}
</a>
</li>
<li class="user_points">
<p>{{Auth::user()->points}} Points</p>
</li>
@if (Auth::user()->type === 'admin)
<li> <a href="#">ADmin Panel</a></li>
@endif // you can check the same way for boss
</ul>
@else
<ul class="login-register">
<li><a class="popup-text" href="{{route('auth.login')}}" data-effect="mfp-move-from-top"><i class="fa fa-sign-in"></i>Sign in</a>
</li>
<li><a class="popup-text" href="{{route('auth.register')}}" data-effect="mfp-move-from-top"><i class="fa fa-edit"></i>Sign up</a>
</li>
</ul>
@endif
after that you need to create a middleware and attach this middlewares for the routes that must be access it only from certain user role
How to create a middleware
php artisan make:middleware CheckAdmin
step 2 type code like this inside your middleware
public function handle($request, Closure $next)
{
if (Auth::user() && Auth::user()->type === 'admin') {
return $next($request);
}
return redirect('/');
}
step 3 register your middleware
open app/Kernel.php
and find protected $routeMiddleware
array
and declare your middleware
'check.admin' => \App\Http\Middleware\CheckAdmin::class,
Now you can attach your middleware to routes like this
Route::group(['prefix' => 'admin, 'middleware' => 'check.admin'], function() {
Route::get('/', 'AdminController@index)->name('admin.index');
})
You can create different middlewares for every userrole you have.
About authentication You need only one login form, validate it with Request helpers and after that login user to his account and depends on user role you will restrict pages, content and everything
Also you can read more about blade directives here LAravel documentation is very clear and easy to understand
来源:https://stackoverflow.com/questions/64124129/how-to-use-the-blade-directive-when-made-a-multiauth-admins-bosses