Snipe-IT v6 401 on Datatables/Ajax loads
I'm documenting this here so I don't lose track of all of the things I've tried here so far. I've been working on the Snipe-IT v6 branch, dragging everything forward to Laravel 8+ (with the goal of us releasing Snipe-IT v6 in Sept/Oct, using Laravel 9 LTS).
I am, once again, getting that fscking 401 unauthorized response on all datatables - or really any ajax calls - (which is, like, 80% of our screens) and I'm honestly a bit stuck. We saw this before with v5, but the primary reasons for that were that there was a missing PHP library OR the token expiration limit was too high and was coming back null on 32-bit systems (which is why we didn't catch that with the v5 release.)
This is more complicated by the fact that the online demo and my local have slightly different configs and some are (evidently) missing some libraries, so I can't even actually tell if this is a real thing or just because of my broken environments.
Side note: MAMP Pro has really been letting me down lately. Their 7.x version didn't have LDAP extensions compiled in (and provided no headers to allow me to manually compile it in), and their 8.0.0 version didn't have GD compiled in. Sigh. I pulled the GD methods from the code temporarily and it didn't seem to help though.
So... I haven't been able to 100% confirm that it's not just silently erroring due to a missing PHP extension, which it WAS doing in Snipe-IT v5, and which massively sucks. It seems impossible to me that there's not a better way to surface those kinds of errors, but it created a lot of problems last time.
If you google this problem, there are a TON of people that have it, but largely for unrelated or widely varying reasons.
- We DO have the
auth:apimiddleware defined, and it is being applied to the API routes, same as it ever was - although I tried it with no middleware assigned, I tried it with justauthand also withguestand no dice. - We DO pass the CSRF token by way of the
window.Laravel = { csrfToken: '{{ csrf_token() }}' };and aconsole.log(window.Laravel);proves that the token is being correctly collected. - I did modify the text returned by
app/Exceptions/handler.php::unauthenticated()to confirm that that IS in fact the method that's being triggered, as the 401 API response text changes when that text changes. - I can confirm that I CAN query the API with a bearer token via Postman just fine, so the API itself is okay, it's only ajax commands - same as we saw in v5.
- I did (of course) clear cache, views, config, and optimize, as well as composer dump, using
php artisan cache:clear; php artisan config:clear; php artisan view:clear; php artisan optimize:clear; - If I try to
dd($request)in thatunauthenticated()method, it runs out of memory - There is nothing in the laravel logs or the PHP logs
- This happens on GET requests and POST ajax requests via the web UI
I'm sure, like last time, it's some tiny step in the upgrade that I'm missing, some obscure line I need to add for some backwards compatibility. Laravel upgrades are always kinda brutal, especially when you've customized the app as much as we have - we do a lot of things that aren't bog standard.
Although I did create a fresh Laravel 8 application and was running into the same issues with the datatables - or almost, anyway....
- 401 when I use the
auth:apimiddleware on the api routes - 403 when I use
apimiddleware - works when I use
webmiddleware but I don't think it's supposed to :-/
I have already explored the solutions listed here:
- https://stackoverflow.com/questions/66867340/401-unauthorized-error-when-calling-laravel-api-trough-ajax-datatable
- https://stackoverflow.com/questions/55531803/ajax-post-request-401-unauthorized-api-laravel (and many more, but I just started to track them here)
Could it be related to laravel passport? Specifically this in the upgrade guide: https://github.com/laravel/passport/blob/master/UPGRADE.md#renderable-exceptions-for-oauth-errors
As you've specified League\OAuth2\Server\Exception\OAuthServerException under $dontReport array which may need updating to a passport specific exception.
GitHub
Laravel Passport provides OAuth2 server support to Laravel. - laravel/passport
@snipe, @uberbrady, @inietov I've found some additional changes which has fixed the Assets by Status and Asset Categories sections on the Dashboard.
Now it comes up with a 500 error for the Assets Data-table on the Dashboard, instead of the 401:
exception: "Error"
file: "/sites/snipe-it/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/MorphTo.php"
line: 186
message: "Class \"Database\\Factories\\App\\Models\\Asset\" not found"
Which I have to admit, I'm struggling to fix. Hoping you might have a suggestion on what to try. See pull request: #9685 for the changes I've applied.
Thanks
@fordster78 awesome, thanks! I know it's marked as a WIP, but can I merge that into the dev v8 branch as is now?
re: factories, I think you need to add to the autoloader in composer.json:
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
More info here: https://stackoverflow.com/questions/63943758/laravel-8-model-factory-class-not-found
Yes, that's fine.
I did try that but it didn't seem to make any difference. The error persisted after doing composer dump-autoload.
Let me know if there is anything else I can help with.
Awesome! Your changes fixed those tables for me as well - super rad! I can't thank you enough - I was really stuck on that for a bit.
I'm digging into the factory issue now.

@fordster78 FYI, I switched branches so the actively developed one for Laravel 8 is now develop-v6-integration. (It's a bear, as we used Laravel Shift to make it a little easier, so there are thousands of changed files. Ugh.)
I fixed the empty dashboard table that was 500ing - I assume you were using php artisan db:seed like I was? I hadn't gotten around to updating some of the factories. Once I fixed the database/Factories/ActionLogFactory.php to use the new class syntax, that table started to load for me.