laravel with multi auth: middleware for multiple user
I have two users currently.
Admin: (the default laravel user from user table)
uses 'middleware'=> 'auth'
Student: (Custom user from custom student table)
uses 'middleware'=> 'auth:student'
Now I have some routes for all users (Route A), routes for admin only (Route B), Route for Students only (Route C), & routes for admin 7 student (Route D)
My question is How can I access the routes using the above-mentioned middleware?
public function display()
{
$title = 'Dashboard';
$totalusers = User::count();
$totalstudents = Student::count();
return view('dashboard.home', compact('title', 'totalusers','totalstudents'));
}
I have doubts on the way I have my handle set on my student middleware:
public function handle($request, Closure $next)
{
if (!Auth::check()) {
return redirect()->route('studentlogin');
}
if(Auth::guard('student')->check()){
return $next($request);
}
return redirect('admin/home')->with('error','You don\'t have admin or student access');
}
@pdlbibek Comment this out like this and let me know if the redirect error goes away.
public function handle($request, Closure $next)
{
// if ( ! Auth::check() ) {
// return redirect()->route('studentlogin');
// }
// if( Auth::guard('student')->check() ){
return $next($request);
// }
// return redirect('admin/home')->with('error','You don\'t have admin or student access');
}
Also, you don't need to auth check here. That is what your auth middleware is doing already.
So remove that. That actually might be your problem. If this middleware is running and you aren't yet authenticated, that would probably cause a loop. Depending on the order your auth middleware is loaded in.
@pdlbibek I think you're conflating auth:api with how your custom middleware is defined.
You didn't define your middleware as auth:student... you defined it as student so use that.
['auth', 'student'] is what you want because you want auth and student middleware. That array will apply both. auth:student means nothing because it isn't defined.
Look at kernel.php that is where you define the aliases for your middleware.
Don't let the colon confuse you. It is simply part of the alias name for auth:api. You could use auth:student if you define it that way... but I would avoid doing that so it doesn't confuse you or another developer later. Stick with simple names if you can.