I have two applications : SPA builded with Vuejs 3 and the api builded with Laravel.
the SPA is running on 127.0.0.1:5173
and the api is running on 127.0.0.1:8000
User can authenticate successfully but I have this error thrown by the api : 404 not found
for the route http://127.0.0.1:8000/home
and it’s not redirected to another page
I know that this is the route redirected after login in Laravel because I have this line in my LoginController
:
protected $redirectTo = RouteServiceProvider::HOME;
and RouteServiceProvider
is set to /home
and in my web.php
I don’t have this route
My question is how I can redirect to the front-end application that can be in another url ?
the login function executed in the SPA :
login(data) {
return new Promise((resolve, reject) => {
axios.get('/sanctum/csrf-cookie').then((response) => {
if (response) {
axios
.post('/login', data)
.then((response) => {
console.log(response)
resolve(response)
setTimeout(() => {
this.loginData.email = ''
this.loginData.password = ''
}, 1000)
})
.catch((err) => {
reject(err)
})
}
})
})
},
.env file :
SANCTUM_STATEFUL_DOMAINS=127.0.0.1:5173
SESSION_DOMAIN=127.0.0.1
cors.php :
'paths' => [
'api/*',
'/login',
'/logout',
'sanctum/csrf-cookie'
],
'allowed_methods' => ['*'],
'allowed_origins' => ['*'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => true,
sanctum.php :
'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', sprintf(
'%s%s',
'localhost,127.0.0.1:5173,localhost:3000,127.0.0.1,127.0.0.1:8000,::1',
Sanctum::currentApplicationUrlWithPort()
))),
web.php :
Route::post('login', [LoginController::class, 'login']);
in api.php
I have logout route and athor routes consumes by the SPA.
2
Answers
I fixed it by passing headers in axios request and after that I redirect with vue routes :
You should redirect with your vue routes not Laravel routes. After you logged in successfully in your axios call you can push your route: