While I was working on my first application that utilizes anonymous users, I ran into another feature of Mendix that I have never used before. The After Sign-In microflow allows for users to carry over data from before they login, through the login process. In the example used in this blog, we allow for a potential user to browse through a list of items available for purchase, before logging in.
Setting up the Data Model
There are two things we need to be aware of here, the first of which is security. Allowing anonymous users access to our application means that anyone can view our data. We must take extra time to ensure that Anonymous users are only allowed to view data and access microflows that they should be able to use. This is very important.
The second is how we are going to transfer the data from the Anonymous user to the logged in user. In this example we will have two associations to the Order entity: one to User (for Anonymous users) and one to Account (for the logged in user).
Next we need to create a view that both the Anonymous and Confirmed users can access. This page will have security set up so that the Anonymous users can only add Products to their order, while Users can Create, Edit, and Delete products, in addition to being able to add them.
Home Page for Anonymous access
In the microflow for adding a product to an order, we will need to ensure that the order is created on the proper association. If we are an anonymous user, we want to use the ‘Order_User’ association, and if not, we want to use the ‘Order_Account’ association:
Now that we have a way for an Anonymous user to add Products to their order, they will need to be able to check out. However, we want them to sign in first. In our ‘Checkout’ microflow, we simply check if they are a ‘User’ or an ‘Account’. If they are an Account, they have signed in, and can simply check out. If they are anonymous, we force them to login:
With this microflow we bring the anonymous users to the Login Page. Then, when they log in, we run our Sign-in microflow. This is set up in the Project Security settings in the modeler:
Project Security Settings
In our Sign-in microflow, we must transfer the data from our Anonymous user to the new signed in User. We do that by retrieving the order from the ‘Order_User’ association from the AnonymousUser, and updating the SignedInUser:
Now if I select some items from the list, sign in, and view my cart, the data was carried over!
Placing an order anonymously (click to view animation)
Have you used this feature before? Are there any cool ways you have taken advantage of this? Share them in the comments below!