I have a fresh installation of Laravel Sanctum API. When I try to log a user in after registration or when submitting the registration form twice, I get an exception with the message "The route dashboard could not be found". I don’t understand why it’s trying to redirect the user to the ‘dashboard’ route. The only place that I can see ‘dashboard’ in my project is in RouteServiceProvider.This seems like a bug too me with Sanctum.
3
Answers
Make sure the dashboard route is added to the routes/web.php or routes/api.php file.
this error shows when you try to register while you are already logged, so make sure to log out before register
You are right,
AppProvidersRouteServiceProviderRouteServiceProvider::HOME
const is set to "/dashboard". If you follow the usage of this const you can see that is used byAppHttpMiddlewareRedirectIfAuthenticated
middlewareThis middleware is aliased as "guest" in the
AppHttpKernel
:And this middleware is used in unauthenticated routes defined by
routes/auth.php
This built-in middleware middleware will try and redirect you to the HOME if you are already authenticated.
I think that there is an issue in Laravel breeze scaffolding (php artisan breeze:install api) that not provide a check on the response type in the
AppHttpMiddlewareRedirectIfAuthenticated
middleware.If you see instead the
AppHttpMiddlewareAuthenticate
middleware here is automatically provided aredirectTo
method that check for the expected response ($request->expectsJson()
):TLDR
In short to work correctly with the
AppHttpMiddlewareRedirectIfAuthenticated
middleware in API based authentication you can add manually the following check in the middleware: