IOS Webapp as a home button: authentication header sent with ajax disabled

I have a webapp that authenticates through a REST APi that responds with a user id and a session token that will be sent with future ajax requests as the main authorization header for the secure api endpoints. This works great in every browser, including iOS Safari. On iOS (iPad) The problem arises when I use the meta tag that supports apple-mobile-web-app and add this application to the main screen: after logging in, even if my authorization header exists (see Its tab in the web inspector network request on my mac), when the first secure call is sent, I get an Authenticaion Required Safari popup asking me to enter my password.

The header is set using the ajaxSetup beforeSend :

 xhr.setRequestHeader("Authorization", "Basic " + base64Value) 

Again, everything is fine on all browsers, in the iOS Safari App mode and in the Home Button mode everything is fine, but when using the apple-mobile-web-app meta application, mecanism authentication is violated.

I searched a lot and did not find a solution.

+4
source share
2 answers

Now my problem is solved. AJAX POST requests get cached in Safari IOS 6, and even if I clear the cache and cookies, the previously requested cached request will get stuck somewhere. I had a problem with another ajax call, but we circumvented it by adding a header without a cache to our REST interceptor, but for some reason the call to enter our API still used the cached response (old session token), but only in WEB CAPABLE mode. Adding a timestamp to the end of the LOGIN URL resolved this issue.

I still don't understand why a particular request will never be cleared from the history / cookies only in WEB CAPABLE MODE.

0
source

I ran into this problem with GET requests. My solution was to add the cache: false flag in jQuery.ajax() , which adds an extra GET parameter with a timestamp to prevent caching. You can use a similar trick in your own code if you are not using jQuery.

POST requests should never be cached. It really will be a bug in iOS.

jQuery.ajax docs: http://api.jquery.com/jquery.ajax/

0
source

Source: https://habr.com/ru/post/1440527/


All Articles