I have create 3 users('Super-Admin','Branch-Admin','User'). I am trying to do that when ' Super-Admin' is login its not gone to the other 2 users dashboard Users('Branch-Admin,User'). But its show a page with "Too many redirect " and when i give URL of any other user in browser it redirect its dashboard from its own dashboard. And same like these other 2 users????
Routes:
Route::group(['middleware' => [ 'auth', 'isNotAdmin']], function(){
Route::get('/profile','ProfileController@getIndex');
});
Route::group(['middleware' => [ 'auth', 'isBranchAdmin']], function(){
Route::get('/branch','BranchController@gettIndex');
});
Route::group(['middleware' => [ 'auth', 'isAdmin']], function(){
Route::get('/Super/admin', 'AdminController@getIndex');
});
View:
<div class="col-xs-12 col-sm-12 col-md-3 col-lg-3">
@if(Auth::check() && Auth::user()->type === 'User')
<ul class="nav nav-pills nav-stacked">
<li role="presentation" class="active">
<a id="bootstrap-overrides" href="/home">
Home
</a>
</li>
<li role="presentation">
<a id="bootstrap-overrides" href="/contact">
Contact
</a>
</li>
<li role="presentation">
<a id="bootstrap-overrides" href="/about">
About
</a>
</li>
<li role="presentation">
<a id="bootstrap-overrides" href="/blog">
Blog
</a>
</li>
<li role="presentation">
<a id="bootstrap-overrides" href="/faqs">
FAQs
</a>
</li>
</ul>
@elseif(Auth::check() && Auth::user()->type === 'Admin')
<ul class="nav nav-pills nav-stacked">
<li role="presentation" @if(Request::path() === 'companies') class="active" @endif>
<a href="/companies">
Companies
</a>
</li>
<li role="presentation" @if(Request::path() === 'branchies') class="active" @endif>
<a href="/branchies">
Branchies
</a>
</li>
</ul>
@elseif(Auth::check() && Auth::user()->type === 'BranchAdmin')
<ul class="nav nav-pills nav-stacked">
<li role="presentation" @if(Request::path() === 'medicines') class="active" @endif>
<a href="/medicines">
Medicines
</a>
</li>
<li role="presentation" @if(Request::path() === 'stock') class="active" @endif>
<a href="/stock">
Stock_details
</a>
</li>
</ul>
@endif
</div>
Middlewares:
BranchAdmin:
class BranchAdmin
{
public function handle($request, Closure $next){
if(Auth::user()->type === 'BranchAdmin'){
return redirect('/branch/'.Auth::user()->branch->id);
}
return $next($request);
}
}
UserIsAdmin:
class UserIsAdmin
{
public function handle($request, Closure $next)
{
if(Auth::user()->type === 'Admin'){
return redirect('/Super/admin');
}
return $next($request);
}
}
UserIsNotAdmin:
class UserIsNotAdmin
{
public function handle($request, Closure $next)
{
if(Auth::user()->type === 'User'){
return redirect('/profile');
}
return $next($request);
}
}
Your middleware logic does not seem to be right. I think you should definitely have too many redirects because of that. I take one middleware as an example.
class UserIsNotAdmin
{
public function handle($request, Closure $next)
{
if(Auth::user()->type === 'User'){
return redirect('/profile');
}
return $next($request);
}
}
What you are saying is that
If the user is of type 'User', always redirect them to '/profile'
Therefore, if a user of type 'User' goes to http://website/profile
, it keeps redirecting them to profile over and over again.
What you should do is actually do what middleware are for: Stop intruders :). E.g: in UserIsNotAdmin middleware, do this
if( !Auth::user()->type === 'User' ){
redirect('/home');
}
return $next($request);
Translates into
if the user IS NOT of type 'User', send them home. Else, let them in.