Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

WebSystem's avatar

Different Error Pages for Authenticated and Unauthenticated Users in Laravel

Hello everyone,

I'm currently working on a Laravel project where I need to display different error pages depending on whether a user is authenticated or not. Specifically, I want to return different views for authenticated users at $request->is('account/*') route and serve them error views from a different path (i.e. 'errors/auth').

My initial approach was to use Auth::check() in the bootstrap/app.php, but it seems that isn't possible as the request hasn't been fully bootstrapped yet, hence, the session and its states are not yet set up and accessible.

Here's a pseudocode example of what I am trying to achieve:

if ($user is authenticated) {
    if ($request is from 'account/*') {
        // Show error pages from 'errors/auth' directory
    }
} else {
    // Show standard 'errors' pages for guests
}

Does anyone know how can I set this up properly in Laravel? Any help or guidance would be greatly appreciated. Thanks in advance!

0 likes
1 reply
tykus's avatar

How did you try to use Auth inside the bootstrap/app.php file?! You have access to the Request, and therefore the User (if available)

->withExceptions(function (Exceptions $exceptions) {
  $exceptions->render(function (Throwable $e, Request $request) {
     if ($request->is('account/*')) {
      if ($request->user()) {
           return view('errors.auth'); // whatever special error page you want
      }
    }
    // otherwise rethrow
  });
})

Please or to participate in this conversation.