I'm creating a multi auth system in laravel where there is two types of users: Admins(created by me) and Users(using the native laravel auth system).
If I login as a User, when I try to access the login page when I'm already logged in, it redirects me back to the dashboard but If I login as an Admin, when I access to the Admin login page again, it let's me login again despite being already logged in as an Admin.
Here is my code for the class RedirectIfAuthenticated:
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
switch ($guard)
{
case 'admin':
if(Auth::guard($guard)->check())
{
return redirect()->route('admin.dashboard');
}
break;
default:
if(Auth::guard($guard)->check())
{
return redirect('/home');
}
break;
}
/*
if (Auth::guard($guard)->check())
{
return redirect('/home');
}
*/
return $next($request);
}
}
Can someone explain me what is happening?