one of my application page is loading some content via ajax in my page (currently 2 requests, after document ready). Many times I receive for this ajax requests the status "401" with response "Unauthorized.". Sometimes on refresh the page (with F5) is it working, Some times ony one request is receiving 401 status.
And less times i receive 500 (laravel is in this case using wrong database credentials, not from .env).
Do you validate your data ?
If so you have to set the authorize method to true in your Requests.
For example:
app/Http/Requets/Authenticate
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
// set to true instead of false
return true;
}
"If the authorize method returns false, a HTTP response with a 403 status code will automatically be returned and your controller method will not execute.
If you plan to have authorization logic in another part of your application, simply return true from the authorize method:"
When making simultaneous XHR requests, session data may be lost due to concurrent requests updating the session at the same time. This is a known issue. One solution may be reducing the number of requests, or use **session locking **. This allows to lock the session file when used, hence forcing all requests to be executed one after the other. This may slow down your app, but eliminate the potential errors.
I also have the same problem on 5.2, which I think is the result of making multiple simultaneous AJAX requests, some of which were to the same URL, which makes no sense I know.
Anyway, the session locking package suggested by chijipon works as a temporary solution for me. I no longer lose my sessions.
my case was the localhost part in the url, I have one page loaded with a vue component that have the vue-resource this.$http.get('api/....') with the proper CSRF headers, called on the component mount()/ready().
Laravel served on the port 3000, the issue was that the localhost:3000 on the url was translated to the IPV6 [::1]:3000 and here was the weird behaviour, sometimes it's worked and the data was loaded successfully, sometimes it's 401 unauthorized.
moving to a custom domain defined on the hosts file pointing to 127.0.0.1 = custom.domain:3000 solved the issue for me.