Which session driver are you using?
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?
Please or to participate in this conversation.