what does your view(...) method in MyController from the private route return?
302 redirect causes an error when using web and auth middlewares
Hello,
I have an app running in Laravel 7, in the app I have a custom routes file which is generated from the database and looks like this:
<?php
use Illuminate\Support\Facades\Route;
Route::middleware(['web'])->group(function() {
Route::get('some/public/page', 'My\PublicController@view');
Route::middleware(['auth'])->group(function() {
Route::get('some/private/page', 'My\Controllerr@view');
});
});
In the current app it works just fine, if I hit the some/private/page I am either displayed the page or redirected to the login page if I am not logged in.
Now I am updating the app to laravel 9, hitting the route some/public/page works fine but when I go to some/private/page I get this error:
Attempt to read property "headers" on string
vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php:191
After trying to figure out what was going on for some time, I made this change to the routes file:
<?php
use Illuminate\Support\Facades\Route;
Route::middleware(['web'])->group(function() {
Route::get('some/public/page', 'My\PublicController@view');
});
Route::middleware(['auth'])->group(function() {
Route::get('some/private/page', 'My\Controllerr@view');
});
This way the 'some/private/page route opens fine the login page, however now I can't login because now the route does not seem to be opening the session so submitting the login form takes me back again to the login form.
Then I also tried like this and the error came back
<?php
use Illuminate\Support\Facades\Route;
Route::middleware(['web'])->group(function() {
Route::get('some/public/page', 'My\PublicController@view');
});
Route::middleware(['web','auth'])->group(function() {
Route::get('some/private/page', 'My\Controllerr@view');
});
By the way, the redirect is happening like this:
HTTP/1.0 302 Found
Cache-Control: no-cache, private
Date: Thu, 26 Jan 2023 12:47:33 GMT
Location: https://domain.com/login
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="refresh" content="0;url='https://domain.com/login'" />
<title>Redirecting to https://domain.com/login</title>
</head>
<body>
Redirecting to <a href="https://domain.com">https://domain.com</a>.
</body>
</html>
Which causes an unnecessary delay and I believe is the actual cause of the problem, before the login page was being rendered without such delay.
Has anyone experienced this issue? Is there something I am doing wrong in the new version? can't I use both web and auth middlewares together?
@s4muel The middleware is not modified but you made me think about the things I did modify, in this case
App\Exceptions\ExceptionHandler
I have some custom exceptions which are documented so the employees can easily fix, I have this:
<?php
namespace App\Exceptions;
use ...;
class Handler extends ExceptionHandler
{
public function render($request, Throwable $e):Response|JsonResponse
{
/**
* Custom error code handler
*/
$exception = $this->prepareException($this->mapException($e));
$message = json_decode($exception->getMessage());
$code = method_exists($exception, 'getStatusCode') ? $exception->getStatusCode() : $exception->getCode();
if(App::environment('production') && in_array($code, Handler::$HANDLED_CODES)){
if($request->expectsJson()){
$error = [
'error' => $code,
'message' => $exception->getMessage(),
];
return response()->json($error, $code);
} else {
return response()->view("errors.custom", [
'code'=> $code,
'message'=> __("error/codes.$code"),
], $code);
}
}
return parent::render($request, $e);
}
}
And what is causing the error is adding the return type to the method, since the parent is responding with \Symfony\Component\HttpFoundation\Response and my method expects Illuminate\Http\Response looks like an error is triggered and the response is somewhere converted to string.
Adding \Symfony\Component\HttpFoundation\Response to the return on my method fixed the problem.
public function render($request, Throwable $e):Response|JsonResponse|\Symfony\Component\HttpFoundation\Response {}
Please or to participate in this conversation.