I have been investigating this problem. As I dig deeper, it keeps turning out to be a little more complex, and I have now come full circle back to SuperTokens.
First, using localStorage does not seem to be a great solution after all. If I didn’t need multiple subdomains, perhaps, but localStorage is unique to each origin. A different subdomain is a different origin, so it won’t work, at least not out of the box. There are hacks, like using postMessage() from iFrame, but I don’t like those kinds of hackish and messy approaches.
I was thinking of just using Firestore because I am already integrated. My first thought was to just keep track of state very simply in a single boolean variable, i.e. isAuthenticated = true | false. Firebase allows me to create an Observable directly on the variable so that I am informed virtually instantly of any changes. That would really make the tracking ulta-simple in my app.
Alas, that won’t work either. Putting aside the fact that I would effectively be duplicating part of the SuperTokens core, don’t forget that the purpose is to track authentication. To read the variable, either the user has to be authenticated or the variable has to be left readable even when not authenticated. The latter is no good, which means that the app can only track the variable when authenticated. This means I could detect a signout, but not a signin.
So it seems to me now that the only reasonable way to do this is to keep polling SuperTokens for the existence of a Session by calling Session.doesSessionExist(). That could work, but it is not ideal because I don't like the idea of having to set up a polling mechanism.
It would be great if SuperTokens provided an observable out of the box (either instead of or in addition to the promise) to allow this kind of tracking in real time, so a client can easily subscribe, and the moment the state changes, the app can take action.
I will set up the polling anyway, but I thought I would share my progress.
wdyt?