Keep data after the app closes
Lynx has no localStorage. LynxLocalStorage takes its place: a key-value store whose values are still there the next time the app starts. For tokens and other secrets, LynxSecureStorage has the same methods and keeps its values encrypted by the phone.
Store and read a value
- Every method returns a Promise.
setItem()resolves once the value is stored. - Values are JSON: objects, arrays, strings, numbers, booleans and
null. They're stored as JSON text, so aDatecomes back as a string, and a value JSON can't hold, such asundefinedor a function, makessetItem()reject. getItem()resolvesnullfor a key that was never set, or was removed.removeItem(key)removes one value andclear()removes them all.clear()on one store leaves the other alone.
Keep secrets in secure storage
Use LynxSecureStorage for anything that would let someone act as the user, such as a session token or a refresh token:
Where the values are kept
- iOS keeps Keychain items when the app is deleted. Someone who deletes the app and installs it again is still signed in. To start them signed out, keep a flag in
LynxLocalStorage, which deleting the app empties, and callLynxSecureStorage.clear()when it's missing. - Secure values stay on the phone. They're readable once the phone has been unlocked after it starts, and don't move to a new one: iOS restores them from a backup only to the same phone, and Android leaves them out of its backups.
- On the web, keys start with
angular-lynx:orangular-lynx-secure:, soclear()leaves the page's own keys alone. Secure values aren't encrypted there: like web sign-in libraries, which keep their tokens inlocalStorageor IndexedDB too, it relies on the browser keeping each site's storage to itself.
Lynx Explorer has no storage for your app, so there both services keep values in memory: every method works, but the values are gone when the app closes. Unit tests do the same. Try storage that lasts on a simulator, emulator or phone with ng serve --platform ios or --platform android. To use it in your own native app, see Embed in a Native App.
Keep a Firebase sign-in
Firebase Auth keeps who's signed in through a persistence. A browser's are localStorage and IndexedDB, which Lynx doesn't have, so without one of your own Firebase can only keep the session in memory, and people sign in again every time they open the app.
Firebase's React Native persistence, getReactNativePersistence(), is only exported to React Native builds, so a Lynx build can't import it. This class does what it does, over LynxSecureStorage:
Pass it to initializeAuth() in place of inMemoryPersistence, from an injection context:
Related
LynxLocalStorageandLynxSecureStoragereference- Share state between pages, for values that only need to last while the app runs
- Persistent Storage example
