eggplantSword's avatar

Using multiple auth: Route [login] not defined.

I have multiple authentication set up but I get this error sometimes, I need to find where there is a reference to the "login" route. This is my code

//Middleware/Authenticate
protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            if ($request->user('web')){
                return route('admin_login_form');
            } else if ($request->user('web_client')){
                return url()->previous();
            }
        }
    }

//Middleware/RedirectIfAuthenticated
public function handle(Request $request, Closure $next, ...$guards)
    {
        $guards = empty($guards) ? [null] : $guards;

        foreach ($guards as $guard) {
            if (Auth::guard($guard)->check()) {
                if ($guard === 'web'){
                    return redirect(RouteServiceProvider::ADMINHOME);
                } else if ($guard === 'web_client'){
                    return url()->previous();
                }
            }
        }

        return $next($request);
    }

//Providers/RouteServiceProvider
  public const ADMINHOME = '/admin/home';

Then in all the Admin Auth Controllers that use $redirectTo

protected $redirectTo = RouteServiceProvider::ADMINHOME;

Then in all the Client Auth Controllers that use $redirectTo

    public function redirectTo()
    {
        return url()->previous();
    }

Here are my routes

Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])->name('homepage');
Route::post('/login', [\App\Http\Controllers\ClientAuth\LoginController::class, 'login'])
    ->name('client_login');

Route::post('/logout', [\App\Http\Controllers\ClientAuth\LoginController::class, 'logout'])
    ->name('client_logout');

Route::middleware('auth:web_client')->group(function () {
    Route::get('/home', [App\Http\Controllers\DashboardController::class, 'client_index'])
        ->name('client_dashboard');
});

Route::prefix('admin')->group(function () {
    Route::get('/', [\App\Http\Controllers\AdminAuth\LoginController::class, 'showLoginForm'])
        ->name('admin_login_form');

    Route::post('/login', [\App\Http\Controllers\AdminAuth\LoginController::class, 'login'])
        ->name('admin_login');

    Route::post('/logout', [\App\Http\Controllers\AdminAuth\LoginController::class, 'logout'])
        ->name('admin_logout');

    Route::middleware('auth:web')->group(function () {
        Route::get('/home', [App\Http\Controllers\DashboardController::class, 'index'])
            ->name('admin_dashboard');
    });
});

I'm not sure where else I have to setup the redirects to so it's not route('login').

What did I miss?

0 likes
2 replies
MichalOravec's avatar
Level 75

route('login') exists inside Illuminate/Foundation/Exceptions/Handler.php

https://github.com/laravel/framework/blob/8.x/src/Illuminate/Foundation/Exceptions/Handler.php#L410

You can replace it in your app/Exceptions/Handler.php

use Illuminate\Auth\AuthenticationException;

/**
 * Register the exception handling callbacks for the application.
 *
 * @return void
 */
public function register()
{
    $this->renderable(function (AuthenticationException $e, $request) {
        return $request->expectsJson()
                ? response()->json(['message' => $e->getMessage()], 401)
                : redirect()->guest($e->redirectTo() ?? route('client_login')); // this is important
    });
}

https://laravel.com/docs/8.x/errors#rendering-exceptions

2 likes

Please or to participate in this conversation.