mecjos's avatar

Route [Login] not Defined Inertia + vite

I have login route in my web routes php but I get this error while redirecting to login route.. It's not api, I think to use Sanctum SPA authantication.. How to configure it properly?

0 likes
12 replies
JussiMannisto's avatar

You didn't show your routes or how you're trying to redirect to the route.

mecjos's avatar

@JussiMannisto

Route::get('/login', function () {
    return inertia('Login');
});

Route::middleware('auth:sanctum')->group(function () {
    Route::apiResources([
        'discussion' => DiscussionController::class
    ]);
    Route::get('discussion/topics/{topicName}');
});

this is my login route and protected routes.. Authanticate middleware gives that error.

class Authenticate extends Middleware

{

    /**

     * Get the path the user should be redirected to when they are not authenticated.

     */

    protected function redirectTo(Request $request): ?string

    {

        return $request->expectsJson() ? null : route('login');

    }

}
hupp's avatar

@mecjos Confirm first with checking php aritsan route:list Check here its route and compare with url generated with your request.

mecjos's avatar

@hupp yes I can see the route in route list. by the way, with auth:sanctum or just auth protection it's same. @im-nazmul I run php artisan optimize after every change.

gych's avatar

If you're using Inertia make sure your login route is in web.php not api.php

When you don't plan to use separate spa with e.g. Vue CLI that needs to connect to your Laravel app you don't need api routes.

Inertia already connects your front end for e.g. Vue with the backend without the need of api routes/calls.

INERTIA Build single-page apps, without building an API. Create modern single-page React, Vue, and Svelte apps using classic server-side routing. Works with any backend — tuned for Laravel.

mecjos's avatar

@gych I use web routes. already wrote in my question. I sat session driver from file to cookie in .env file.. I don't know what are other configurations.. according to doc it must work only with web routes.

gych's avatar
gych
Best Answer
Level 29

@mecjos Ok I see that you use route('login') in Middleware

Therefore you need to update route like this and add name to it

Route::get('/login', function () {
    return inertia('Login');
})->name('login');

route() doesnt use the route path but route name

mecjos's avatar

@gych yes it worked with name.. thank you. Must session_drive be cookie for sanctum web routes ? another question.)

1 like
gych's avatar

@mecjos Session driver cookie is suggested in sanctum documentation: https://laravel.com/docs/10.x/sanctum#how-it-works-spa-authentication

I've seen other people use database session driver but I have no experience with it for sanctum https://stackoverflow.com/questions/72735074/laravel-sanctum-and-cookie-based-session-security

Are you going to make a separated SPA with e.g. Vue CLI for your application? Because when using Inertia its not necessary.

Please or to participate in this conversation.