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

bellenoire's avatar

Laravel API no longer authenticates after server migration

I have a Laravel API that is currently hosted on a GoDaddy server. GoDaddy needed to migrate this application to a smaller server, and once they did, I am able to authenticate with the API with no errors; however, when I send a subsequent request with the API token, I get a 401 error from the API.

Authentication Request

POST https://blackistory.com/api/v1/authenticate
BODY {"api_token":"lKqsFGhhMfKtr9f0zB4szYiFtly9Y2HJOiDOr1Gnu2eERbQlsPXXjZwZb1RZ"}

Authentication Response

{
    "meta": {
        "status": 200,
        "success": true,
        "results": 0,
        "endpoint": "https://blackistory.com/api/v1/authenticate"
    },
    "data": {
        "player": {
            "id": 667,
            "username": "bellenoire2005",
            "email": "[email protected]",
            "is_logged_id": 1,
            "api_token": "lKqsFGhhMfKtr9f0zB4szYiFtly9Y2HJOiDOr1Gnu2eERbQlsPXXjZwZb1RZ"
        }
    }
}

Authenticated API Request

curl --location 'https://blackistory.com/api/v1/categories' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer lKqsFGhhMfKtr9f0zB4szYiFtly9Y2HJOiDOr1Gnu2eERbQlsPXXjZwZb1RZ' \
--header 'Cookie: XSRF-TOKEN=eyJpdiI6ImQwVk5TdkYwUTFtU25qb3RLYmR3dXc9PSIsInZhbHVlIjoiMi9tUWdWUDFjK0ozQVlNMzBQTVdJUmdaT2pjOCt3akMzZ0t5Z1huUlB0ekI1Q0drYlY0WEpvWkk3MlJWcVVrTVZ0SXdwRW14NnpCb2RMTDZMV1ZGa3crMmdJbXhZeU1GUGcrN1JBMytzQitEWWlkNzNqVWVUMTVoSkw0Z1BSOGsiLCJtYWMiOiIzN2ZkNWEzMjQxNmI3MTBiNjRhN2EzOGFhZTEzN2FkOTY0OTY0NTQ3NmE4YjU1NWQ0NDE4NjAzYTg3MzYzN2E1IiwidGFnIjoiIn0%3D; blackistory_session=eyJpdiI6InYxUHE0ZWlURUorZmxNODdGNDFJSnc9PSIsInZhbHVlIjoiWTAzT09ab2lyTmdnaUFqRllsSUIreGlVZ2pKU0xaUzdyRjFlZGxCNGd5cEsya2k3M1hCMndJL0xxU1RlNnFWSkVxWVVpMDhjcHRzNVJmY0wrbTZyU0MvWHdaL2NCNTA2ZVpKVVJvM2wzemJBUGExbEJES2JWNXIzblFkUmw4VnkiLCJtYWMiOiI0OTIyMmI0NjU5ZDZmMGE5ZDY5YTVlMzE5ZjZlN2Q2MmJkNjQyMjA5YmFlYzE1NDE1YmE4Zjc2ODc0MTI2NDVlIiwidGFnIjoiIn0%3D'

Authenticated API Response

{
    "error": "Unauthenticated."
}

I was able to tell that the requests were not being authenticated correctly due to the API falling into the following logic:

app\Exceptions\Handler.php

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

        if ($request->is('admin/*')) {
            return redirect()->guest(route('login'));
        }

        return redirect()->guest('/login-player');
    }

I checked the .htaccess for the site, and as far as I can tell, it hasn't changed:

<IfModule mod_rewrite.c>
  <IfModule mod_negotiation.c>
      Options -MultiViews -Indexes
  </IfModule>
     RewriteEngine on
     # serve existing files in the /public folder as if they were in /
     RewriteCond %{DOCUMENT_ROOT}public%{REQUEST_URI} -f
     RewriteRule (.+) /public/ [L]
     # route everything else to /public/index.php
     RewriteRule ^ /public/index.php [L]
 </IfModule>

I can't reproduce this error using a local version of the same API, so it seems server-dependent. I have even downloaded the API from the site and run it using php artisan serve and haven't had the same issues. I have run composer install and have had no errors when running it. Could there be some server-side configuration that I am missing?

0 likes
11 replies
gych's avatar

Which session driver are you using?

gych's avatar

@bellenoire Check if the correct file permissions are set for the storage/frameworks/sessions folders.

bellenoire's avatar

@gych So, the storage/frameworks/sessions folder is set to 755, all the files in there are set to 644

Snapey's avatar

@bellenoire file permissions are still relative to the user. Its not sufficient to know the numbers unless you know the owner of the file/directory and the user trying to access them.

Snapey's avatar

if you can, check the time/timezone on the server. Many authentication methods can be thrown off by time differences.

obrunopolo-47830258's avatar

I'm having this exact same issue. It's undebuggable. I'm using laravel 9. The same installation worked in AWS server, and still works locally. On new webserver, simply doesn't work. Did you find a satisfatory answer to your issue? Maybe it could be applied to my case. Thank you

Please or to participate in this conversation.