nova-issues icon indicating copy to clipboard operation
nova-issues copied to clipboard

[11.x] Route [login] not defined due to missing auth routes registration

Open codebarista opened this issue 1 year ago • 9 comments

  • Laravel Version: 11.0.3
  • Nova Version: 4.33.0
  • PHP Version: 8.3.3
  • Database Driver & Version: sqlite
  • Operating System and Version: macOS Sonoma Version 14.4
  • Browser type and version: Google Chrome Version 122.0.6261.129 (Official Build) (arm64)

Description:

A new installation of Laravel 11 and Nova 4 does not redirect the nova.path to the login page. The login url itself works.

Detailed steps to reproduce the issue on a fresh Nova installation:

  1. Install Laravel 11: laravel new awesomeness
  2. Install Nova 4: cd awesomeness && composer require laravel/nova
  3. php artisan serve
  4. Navigate to: http://127.0.0.1:8000/nova

Expected behaviour

Redirect to: http://127.0.0.1:8000/nova/login

Current response

Symfony\Component\Routing\Exception\RouteNotFoundException
Route [login] not defined.

Possible "Fix"

Add auth routes to routes/web.php

<?php

use Illuminate\Support\Facades\Route;

Route::get('/', static function () {
    return view('welcome');
});

Auth::routes();

Unfortunately, I didn't get to the root cause, or perhaps I overlooked a configuration?

codebarista avatar Mar 12 '24 22:03 codebarista

Getting the same issue with clean Laravel 11 install

eimantaaas avatar Mar 13 '24 11:03 eimantaaas

I've just encountered the same issue.

  1. Fresh instal of Laravel 11 and Nova 4
  2. Local development using Laravel Herd
  3. Running PHP 8.2

I've also added the Auth::routes(); in routes/web.php which temporary fixed it for now.

jivanrij avatar Mar 13 '24 11:03 jivanrij

I solved in this way on my bootstrap/app.php:

<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Foundation\Http\Middleware\TrimStrings;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->append(TrimStrings::class);
        $middleware->redirectGuestsTo(fn () => route('nova.pages.login'));
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();

filipposallemi avatar Mar 15 '24 09:03 filipposallemi

I don't have this problem because I use a different authentication system, but if the link works cant you just update the config?

I use the below config in /config/nova.php

    'routes' => [
        'login' => '/auth/redirect',
        'logout' => '/auth/logout'
    ]

So maybe you could do something like:

    'routes' => [
        'login' => '/nova/login',
        'logout' => '/nova/logout'
    ]

Don't know if it works, but maybe this helps finding the problem.

niekdemelker avatar Mar 25 '24 19:03 niekdemelker

@crynobone should we make PR to nova-docs?

nastoychev avatar Apr 10 '24 08:04 nastoychev

Having this problem too. The nova/login route is there when I run artisan route:list. Seems like a Nova bug more than a documentation issue? I feel like out of the box Nova should be able to handle redirecting unauthenticated users from /nova to /nova/login. Also raised here https://github.com/laravel/framework/issues/51113

cameronmurphy avatar Apr 18 '24 08:04 cameronmurphy

There's scenarios where:

  1. application has a "frontend" login for normal user.
  2. application and Nova use separate auth guard.

For above cases, Nova shouldn't be the default redirected page for login.

crynobone avatar Apr 18 '24 08:04 crynobone

Fair, it's just strange that setting up Nova on a 10.x Laravel project does the redirect for you and on a 11.x Laravel project it doesn't.

cameronmurphy avatar Apr 18 '24 11:04 cameronmurphy

There's scenarios where:

  1. application has a "frontend" login for normal user.
  2. application and Nova use separate auth guard.

For above cases, Nova shouldn't be the default redirected page for login.

Yes... Here is how I use it now. So I make current redirect if it's only nova request.


<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Foundation\Http\Middleware\TrimStrings;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->redirectGuestsTo(function (Request $request) {

            $isNovaPath = $request->is(str_replace(search: '/', replace: '', subject: config('nova.path')));

            return $isNovaPath ? route('nova.pages.login') : null;
        });
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();


nastoychev avatar Apr 18 '24 12:04 nastoychev

There's a new prompt in nova:install command to solve this. For existing application above code work or you can update App\Providers\NovaServiceProvider:

/**
* Register the Nova routes.
*
* @return void
*/
protected function routes()
{
    Nova::routes()
-       ->withAuthenticationRoutes()
+       ->withAuthenticationRoutes(default: true)
        ->withPasswordResetRoutes()
        ->register();
}

crynobone avatar May 23 '24 00:05 crynobone

Had the same problem with a FRESH install. Why do I have to solve this by searching and finding this here?

saschaende avatar Aug 18 '24 19:08 saschaende