Create an Autologin using Deeplinks with Mendix

Mendix Community
Published in
8 min readApr 23, 2021

--

Creating an automatic login feature can be quite a challenge. A feature which is intended to make approval processes smoother and more efficient, can quickly make a developers life difficult. From regular login features, to more complex approval processes without even the need for a login, Deeplinks are incredibly powerful if used correctly. In this demo I would like to show you how we made an autologin with existing widgets and modules available from the Mendix Marketplace.

First of all, thanks to those who have contributed on the market place modules for

All of these are modules are needed in this demo project. With that being said, let’s get started using Mendix Studio Pro 7.23.20 (LTS).

https://bit.ly/MXW21
https://bit.ly/MXW21

Create a blank project and import modules and widget which I mentioned above. Once you download the AutoLogin module, it already contains the javascript snippet.

Now we have all the required modules but there are also 2 errors.

The errors are just about the popup layout. You can change these pages to use the Atlas popup layout instead.

Then the errors will be gone.

Next we can configure the deep link to automatically start when the app does, using the after startup microflow setting under the project settings.

From this point you also need to add an autologin deeplink request handler with a return value as a boolean.

Next we need to setup the deeplinkhome microflow on the app’s Navigation.

Take a look at the DeepLinkHome microflow, we should implement something here which tells the application to redirect specific user roles to specific pages.

From myfirstmodule, create a new microflow, name it ACT_GoToHomePage. Select “Inheritance Split” (light green diamond shape) and map it with the currentUser object. This CurrentUser is the user who is accessing the web page.

Add an “Action Activity” box, select Cast Object

Type the output name as “Account”.

Right click on the red arrow point from CurrentUser to CastObject, then select Condition value, and select Administration.Account.

There are another 2 paths you need to define for the inheritance split to get rid of all the errors.

From the account Entity, we need to make one Enum field for checking account types in order to route to correct page.

We make 2 pages and name them as UserPage_Overview, and AdminPage_Overview.

Just make a simple title to see which page is used for user or admin.

Now back to the microflow ACT_GoToHomePage. We cast an object account, and setup AccountType to navigate to different pages.

In the security settings, you can select Production.

Still in the security settings, under the tab “User roles”, we need to add one more role, name it “Anonymous”. The AutoLogin module has already had this role since the beginning, so no need to select again.

Anonymous security setting

User security setting

Admin Security setting

Final Security Setting

In Navigation menu, you will have to setup the Administrators navigation as follows so they can access and configure the project settings.

Now add the microflow ACT_GoToHome to the deeplinkhome microflow.

Everything is almost done, however we still need one microflow for deeplink with email parameters.

At this point you can save and run locally. Just remember that when you enable anonymous access, it means you need to have a login screen for anonymous users to login. If you run locally now, you will see a white screen, because there is no landing page for anonymous setup. To fix this we need to implement something on ACT_GoToHomePage. The Anonymous and MxAdmin roles are initiated with system.User, but the difference is ,one is Anonymous but MxAdmin is not and has an Admin role which Account Object Inheriting to System.User.

Save and run again, we can see that after sign in MxAdmin will be at another page.

Ok, so far so good, we can setup deeplink and create test account for now.

Login as admin so that you can navigate to model reflection overview and refresh the object list with mx reflection.

Then create a new deeplink which we will connect with the microflow “ACT_AutoLoginWithEmail” which we have created in the previous steps.

The result of deeplink is
http://localhost:8080/link/approve?email=YourValue

Now go to account, create 2 new users with 2 user roles to test the auto login.

So I have 2 accounts : tony1(User) and tony2(Admin).

If Ipass the deeplink with format http://localhost:8080/link/approve?email=tony1@tbn.co.th. It should navigate to Welcome User Page.

Here we go

http://localhost:8080/link/approve?email=tony2@tbn.co.th

Something else that you may need to know: this AutoLogin feature also works in Mendix 8, but some users will be faced with the error below.

To resolve this, make sure your project is upgraded at least 8.18.4 to overcome the issue. After upgrading you will still see the error message as shown below.

Go to Mendix Marketplace and search for “javascript snippet” download version 1.3.0 and place it in widget folder of the project.

After copying and overwriting it, please synchronize the project one more time by pressing F4 or going to “Project” in the top left hand menu inside Studio Pro and click on “Synchronize Project Directory”.

You will see one final error, caused by the widget definition changing.

Right click on the widget and select “Update all widgets”.

Run locally again and test.

There should be resolved and the user should be directed to the correct page when they use the deeplink to login.

Here is the github repo link for both version 7 and 8 project.

Good luck, and thanks for reading!

Read more

From the Publisher -

If you enjoyed this article you can find more like it at our Medium page or at our own Community blog site.

For the makers looking to get started, you can sign up for a free account, and get instant access to learning with our Academy.

Interested in getting more involved with our community? You can join us in our Slack community channel or for those who want to be more involved, look into joining one of our Meet ups.

--

--

Mendix Community

Certified Mendix Expert MVP, Data Scientist, and Technical Practitioner @ TBN Software