Reputation:
I'm using laravel 5.7. I have a users table with a column user_type where
0 = default users
1 = Professionals
2 = Facilities
8 = Employees
9 = Managers
10 = Administrator
And another column which is boolean is_premium which returns either true or false.
I have created a middleware for admin
namespace App\Http\Middleware;
use Closure;
use Auth;
class IsAdmin
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (Auth::user() && Auth::user()->usertype == 10) {
return $next($request);
}
return redirect('/administration/dashboard');
}
}
And i have used it in the routes/web.php file as
Route::group(['prefix' => 'administration', 'middleware' => 'admin'], function(){
Route::get('/dashboard', 'Admin\DashboardController@index')->name('admin.dashboard');
});
When i visit the admin dashboard route it says
ERR_TOO_MANY_REDIRECTS
I know there is something im doing wrong...what is it?
And im assuming to create middlewares for other usertypes too and use in the routes file. Is this the right way to do?
Upvotes: 0
Views: 300
Reputation: 1856
Try false logic first:
...
public function handle($request, Closure $next)
{
if ( ! (Auth::user() && Auth::user()->usertype == 10) ) {
return redirect('/home');
}
return $next($request);
}
...
Upvotes: 1