How to set a client var in reactive after login


-- Context

In my Reactive app, each user can have several Account. The idea is that, at each time, they are using the app in the context of a single Account. I provide mechanisms in the UI to add / remove and switch the working Account.

Just after login, the user should continue working in the last working Account.

-- Approach

I created a Client variable CurrentAccountId that holds the working Account Id. I use it to filter data in aggregates in screens. 

When I switch the working account, I also update the CurrentAccountId.

I also created an entity UserPref that has an AccountId attribute. When I switch the working Account I also update the user prefs.

-- Issues

If it was traditional web app, I would define a session variable (instead of the client var), and in the Login.Login screen action I'd read the Account from the UserPref and store in the session var.

How do I accomplish the same in Reactive? In the "equivalent" DoLogin server action I don't have access to client vars.

What I did was to in each screen OnInitialize to call a client action. This action checks if the CurrentAccountId client var  is nullidentifier() and retrieves it from the UserPref in the database if so. 

Although this seems to be working, I guess there are better options. Any suggestions? Thanks.



Look references in Users, maybe help you.


Hi 8bp,

I would just put that logic in the layout instead of each screen.  

I tried and it's working, but euhm I'm getting a performance warning, but since the server action only gets called in case the client variable is not set (i.e. at login of a user), I guess it's acceptable to do that in the OnInitialise or OnReady.

But that's just a small improvement over what you already have, so maybe there are much better options out there...

see attached oml for example, it's in the OnInitialise of the LayoutTopMenu block.  I added a message to see how often he actually goes to server for it, you obviously wouldn't have that in a real app.

The client variable is used as background color of title in all screens.



Hi Dorine

Thanks a lot for the effort! 

Actually I was thinking in doing something similar: adding a specific block to take care of this user pref stuff, and passing an argument stating if the calling screen requires a working account already defined (some screens don't need it). I also thought in raising a specific exception in that block, in case the condition is not met, and in the flow catch the exception and redirecting to the switch accounts screen.

 I'll look at your sample. 

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.