Restricting user authentication based on criterias

I am using the plugin RainLab.User in its latest version (3+) to handle registration and authentication.

I have two themes that can use the login page.
the 1st theme is for a user type 1;
the 2nd theme is for a user type 2;

Each theme has its own login page.

I want to restrict user authentication in each theme based on the user lets say his user group belonging. How can i go about that ?

Im looking at doing some magic in the function onEnd() in each login page to test their user group
belonging.

Is there any other better approach to that ?

I would probably handle this in Twig, using a partial inside the layout

{% if user.type1 %}
    {% do redirect('type1/login/page') %}
{% endif %}

And then repeat something similar for type 2

1 Like

great idea.

I used this in the app layout

{% if user and false == user.inGroup('mygroup') %}
    {% do redirect('/403') %}
{% endif %}
1 Like

just small reminder to set proper status code, because you wont get indexed redirects etc.

{% do redirect(ā€˜/403ā€™, 403) %}

good point, thanks @snipi

conversely, Iā€™d like to add a specific group when a user registers in one of the site of my multisite setup, how can I achieve that ?

I can listen to the event rainlab.user.register but it will fire every time a user register. and i want to control it only in a particular website attached to a theme because other site within can also register user.