I am working with Laravel 11, and I'm using Sanctum for API authentication. However, I am encountering the error:
Method Illuminate\Auth\RequestGuard::viaRemember does not exist.
This error occurs due to the inclusion of \Illuminate\Session\Middleware\AuthenticateSession::class
in my app\bootstrap\app.php
file.
Removing AuthenticateSession::class
from the middleware to bypass the error, but I’m unsure if this is the correct solution.
Here's how my app\bootstrap\app.php
looks:
<?php
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__ . '/../routes/web.php',
api: __DIR__ . '/../routes/api.php',
commands: __DIR__ . '/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
$middleware->statefulApi();
$middleware->group('web', [
\Illuminate\Cookie\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\Illuminate\Foundation\Http\Middleware\ValidateCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Illuminate\Session\Middleware\AuthenticateSession::class, // Problematic line
]);
$middleware->group('api', [
\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
]);
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create();
I need to resolve this issue with AuthenticateSession::class
while ensuring that Sanctum-based authentication works correctly in Laravel 11