@JustinC, thanks for the kind comment on Floppy. As for your question, I think I understand correctly that you’re looking to handle the following scenario:
- You have some page(s) where the user might visit them in the logged out state.
- They might then login via OAuth (or even any method) and become logged in (and the page will reload, of course, in the case of OAuth)
- There is some workflow that you want to run when this happens, but only upon first logging in, not upon visiting the page in the already-logged-in state.
So, I can’t test this exactly very easily (my test app doesn’t have Oauth login setup, only email/password and I don’t feel like adding that feature at the moment), but the following technique should work.
Here’s the best and most reliable way to do this, after a bit of testing:
What we’re going to do is use a Floppy element configured for session storage to write a boolean value (a yes/no) that indicates if the page has been visited while logged out. So, add a new Floppy element to your page and set it up like this:
Storage type: sessionStorage
Auto Read Storage: yes
Type of Values: yes/no
Scalar Key Name: visitedLoggedOut (you could pick your own name, but this is how I would do it)
List Key Name: clear this field out, we won’t be using a list key
You’ll note that I used the inspector to give this element the name “Floppy Login Detector”, because that’s what it’s going to do for us.
All other fields should be default.
Now, what we are going to do with this Floppy is as follows:
- If we ever see that the user is logged out and the key does not exist, we will write the key with the value “yes” (this indicates that the user visited the page in the logged out state).
- If we ever see that this key is “yes”, but the user is logged in, this indicates that the user moved from the logged out to logged in state. In that case, we will remove the key and then do whatever actions we want to do “when the user first logs in”. But those actions will not be executed if the user came to the page already in the logged in state.
Cool. The best, fastest, most reliable way to do this – which should work properly whether the user logs in/out via OAuth or via email/password – is to create two “Do when a condition is true” events, as follows:
First, create a workflow that writes the key when the user is logged out:
The condition is Floppy Login Detector's Scalar Value (Storage) is "no" and Current User is logged out
. Note that we also set this to run “Every Time
”.
You can do other things in this workflow (like, I used debug buddy just to log a console message), but the important part is to set the key (which is called visitedLoggedOut) to yes:
That is, we want to set the default scalar key to yes, so we say “Store Scalar: yes”, we can leave the key name blank (use the default), and “Scalar Value: yes”. We do not need to store the list key so I set it to “no” (but since there is not default list key, you don’t actually have to do that, but it’s cleaner this way).
So, whenever we see that the user is logged out, but does not have this key written, we write it. So, this will handle the case where the user visits the page in logged out mode, but also the case where they visited logged in and then went logged out. (So, if they logged in again, we would see once again that went from the logged out to logged in state.)
The second workflow handles the case where we see that the user was logged out, but now has become logged in. Again this is a “Do when a condition is true” workflow that triggers “Every Time”:
The condition is: Floppy Login Detector's Scalar Value (Storage) is "yes" and Current User is logged in
. Again, run this is set to “Every time”.
And what we do when we see this condition (which means that the user has moved from the logged out to logged in state), is we remove the “visitedLoggedOut” key using the Remove Keys action:
And then, in further workflow steps, we can do whatever it is we want to do when the user has logged in, having been previously logged out.
And these will only run when this change of state has occurred, not when the user visits the page and was already in the logged in state.
The reasons that we use session storage for this (rather than local or IndexedDB) is that:
- This key will only survive for the current session and will clean itself up when the session ends, but
- Session storage keys do survive a page refresh, as when happens with an Oauth workflow (the session stays alive during that hokey-pokey).
While I haven’t tested this with an OAuth login, it should work for you just fine. It also, as I noted before, works with email/password login/logouts. So it covers all the bases.
The reason that I’m using “Do when condition is true, Every time” type of triggers is that we don’t have to worry about whether the “User Logged In” or “User Logged Out” event happens before Floppy Login Detector is initialized (and vice-versa). These conditions seem to evaluate reliably and let us do the subsequent actions just fine.