Currently seeking new career opportunities in web development, particularly with Laravel, Hire Me

Laravel API change unauthenticated message

David Carr

Laravel Framework API Tutorials

When making API calls to Laravel when a user who is not authenticated makes a call a 401 status code is returned and the following response:


Which I find a little strange it's an error so message should say error in my opinion.

This can easily be changed by adding a customer unauthenticated method to app/Exceptions/Handler.php:

protected function unauthenticated($request, AuthenticationException $exception) 
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);

    return redirect()->guest('login');

Now the response will say error instead of message.

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!


My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.