Facebook browser on iOS: problems with localStorage

Viewed 1097

Localstorage for our webapp (react) behaves strange when the site is opened in the Facebook browser on iOS and android. We have an authentication flow that depends on storing a token in localstorage and reading it. The symptoms we see when our site is opened in the facebook browser (a user clicks on a link to our site on Facebook) is that the auth flow fails on the first attempt at login but works on the second attempt at login. It appears like the localstorage is asyncronous. At least it behaves differently from other browsers like Chrome, Firefox and Safari.

I have tried to do a setTimeout of up to 700 ms before redirecting after login, but it still fails to read the token from localstorage. I am however able to do localstorage.getItem(key) immediately after localstorage.setItem(key, value) and get the expected result.

1 Answers

If somebody has problems with localStorage in Fasebook in-app browser, please make sure that you set strings to variables in localStorage (not numbers, boolean, etc.). This is a simple rule but sometimes you forget about it when you don't get errors.

This my code worked in smarty Chrome but didn't work as expected in Facebook browser because the exp variable was a number:

window.localStorage.setItem("exp", exp);

But this code worked well in both browsers:

window.localStorage.setItem("exp", ''+exp);
Related