Doing custom stuff when a user logs in

By 18 June 2014 Mendix 12 Comments

If you want do some custom checks when a user logs in, it is possible to override the Mendix login mechanism.

With this, you can do a lot of useful things. For example:

  • Implement a custom user blocking mechanism, e.g. block all users that are linked to a certain company.
  • Only allow users to log in from certain IP addresses
  • Dynamically assign user roles when logging in
  • Etc. etc. (leave your comment below for more ideas!)

To override the standard login, we need to do some Java coding. Create a new Java action ReplaceLoginAction, with no parameters. Call this Java action on startup of the application, by adding a startup microflow, and adding a call to this Java action there. Add the following Java code to the Java action:

You need to added 2 Java class files to your module’s Javasource folder:

LoginActionListener.java:

And CustomLoginAction.java:

This executeAction() method will be called when a login-attempt is made, and will do the standard checks that Mendix does when logging in, plus call a microflow LoginAllowed in which you can do your own custom stuff. This can, for example, check whether the application is open for non-admin users, like this:

Microflow LoginAllowed

Microflow LoginAllowed

This microflow will be called by the CustomLoginAction in system context, which allows you to do almost anything! Just be careful for security and performance issues if you do too much here 🙂

12 Comments

  • […] Note: You could also implement a more sophisticated blocking mechanism yourself, by added your own attribute(s) to Administration.Account, an then do a check on these attribute when the user logs in, as explained in my previous post. […]

  • Michael says:

    Hi Bart,

    Trying to implement this function but i receive an error on the first line.
    LoginActionListener loginActionListener = new LoginActionListener();

    Any ideas?

  • Olivier says:

    Hi Bart,

    Thanks for the write up and explanation, great help!
    However, with Mendix 6 this no longer works as it uses Deprecated libraries. Could you update this post?

    thanks!

  • Mohan says:

    Is it still working with Mendix7? As mentioned above I tried to add this to startup microflow some how startup microflow is not triggered on app startup. So I called the microflow from default home page but the listener code is not called on signin button click.

  • Alexandre says:

    The way to register a listener has changed. You new to use this :

    Core.getListenersRegistry().addListener(loginActionListener);

  • Sharon says:

    Hi Bart, I’ve tried to implement this in Mendix 8. I’m not a java expert so it took a little work.
    I can see this as being extremely powerful, however i can’t seem to get the correct error message back to the user after logon (if allowed is returned false) after an exception is forced.:
    boolean allowed = Core.execute(getContext(), “MyModule.LoginAllowed”, parameters);
    if (!allowed)
    throw new AuthenticationRuntimeException(“Login FAILED: user “+this.userName);

    I’ve debugged and it seems my custom microflow is returning the correct boolean whether or not i want the user to be logged in.
    I get a generic username and password error message, instead of the above.
    Any ideas?

    • MD says:

      Bump.

      Any ideas on this? Even the default Mendix login machinery returns a generic “Invalid username or password” error when the user is temporarily blocked, rather than something more meaningful like “Maximum login attempts exceeded – try again later”.

      Being able to present a custom response message seems like a trivial thing – why does Mendix have to make it so obscure/difficult??

  • Pavan says:

    Is this code still relevant and working for Mendix 8+? I am trying but get error

    —–

    {“type”:”LoginAction”,”user”:”superadmin”},”after”:[],”type”:”EventExtendedAction”}’, all database changes executed by this action were rolled back
    at com.mendix.basis.actionmanagement.CoreActionHandlerImpl.processErrorState(CoreActionHandlerImpl.scala:151)

    Caused by: com.mendix.core.CoreRuntimeException: An error occurred while instantiating action ‘CustomLoginAction’
    at com.mendix.basis.actionmanagement.ActionRegistry.$anonfun$instantiatorFor$1(ActionRegistry.scala:59)

    Caused by: java.lang.IllegalArgumentException: wrong number of arguments
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

  • Żaneta says:

    Can I navigate to another page (for example when I want to show another form with code for 2FA) inside CustomLoginAction? I was trying to use microflow with ShowPage action, but it shows Login failed message on Login Form and nothing happens.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.