Laravel 5.8 Auth::guard($guard)->check() always return false


  • Web Development

    I have two separate logins using guards which customer & adminuser. I have checkRole middleware for adminuser login and it works perfectly. I used guest middleware (RedirectifAuthenticated) to customer login. When customer login check with Auth::guard($guard)->check() it always returns false. But Auth::guard('customer')->attempt returns true. I can't find a solution.

    here is the login controller for customer

    class LoginController extends Controller
    {
    
        use AuthenticatesUsers;
    
        public function __construct()
        {
           $this->middleware('guest:customer')->except('customerlogout');
        }
    
        //login the customer
        public function customerLogin(Request $request)
        {
            $email = $request->get('email');
            $password = $request->get('password');
            
            
            //Getting user inputs and authenticate the customer
            $status = Auth::guard('customer')->attempt([
                'email' => $email, 
                'password' => $password ] 
                );
    
            //Check login  
            if ($status) {
                //if login pass,redirect to customer account page
                return redirect()->intended('/customer_account');
                
             }else{
                 //if login fail,redirect back to home page
                 return redirect()->back();
             }
        }
        
        //logout the customer
        public function customerlogout(Request $request)
        {   
            Auth::guard('customer')->logout();
            $request->session()->flush();
            return redirect('/index');   
        }
    }
    

    here is the guest middleware

        public function handle($request, Closure $next, $guard = null)
        {       
            if ($guard === "customer" && Auth::guard($guard)->check()) {
                return redirect('/customer_account');
            }
            if (!Auth::guard($guard)->check()) {
                return redirect('/index');
            }
            return $next($request);
        }
    

    Any help is appreciated, thanks!



  • can you dd($guard) and check the $guard value ?


  • Web Development

    @ciaompe yh i checked. it returns the correct guard value


  • Linux Help