Lanka Developers Community

    Lanka Developers

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Shop

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

    Laravel Framework
    laravel php authentication login
    3
    4
    1479
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • hashan
      hashan Web Development last edited by

      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!

      1 Reply Last reply Reply Quote 0
      • ciaompe
        ciaompe last edited by

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

        hashan 1 Reply Last reply Reply Quote 0
        • hashan
          hashan Web Development @ciaompe last edited by

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

          root 1 Reply Last reply Reply Quote 0
          • root
            root Linux Help @hashan last edited by

            @hashan

            check this out

            https://laracasts.com/discuss/channels/laravel/why-my-custom-authentication-with-guard-not-working

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post

            0
            Online

            3.6k
            Users

            1.3k
            Topics

            5.3k
            Posts

            • Privacy
            • Terms & Conditions
            • Donate

            © Copyrights and All right reserved Lanka Developers Community

            Powered by Axis Technologies (PVT) Ltd

            Made with in Sri Lanka

            | |