Home > Blockchain >  laravel middleware always returns to login page, even if authenticated
laravel middleware always returns to login page, even if authenticated

Time:07-16

This problem appeared after I added the middleware on the route, I used the default auth middleware. I'm here to manually create a controller according to the laravel documentation, I checked the process inside the controller and the status is successfully logged in with the 'dd' syntax, but every time I add the 'auth' middleware to the middleware it always returns me to the login page, here in the user table i changed 'id' to 'nim'.

this is my users table

this is my authenticate procces in my controller

this is my route

and this is my default auth middleware named authenticated.php

I'm very grateful if anyone helps me find a solution, thanks dev!

CodePudding user response:

This condition in your middleware is causing the problem:

if (!$request->expectsJson()) {
   return route('login');
}

If in the header of your request you don't send 'Accept': 'application/json' you will be redirected to your login page.

Just correct your condition, or simply remove this condition, and you'll be fine.

CodePudding user response:

You should change your condition in your middleware with the below code if you don't want to send the application/json request.

if ($request->expectsJson()) {
     return route('login');
   }

If you don't have 'Accept': 'application/json' header in your request then add 'Accept': 'application/json' header in the request and let the middleware code as it is.

  • Related