Discuss

Redirecting on Login Based on User Roles

On this page

Sometimes you need to redirect users on log in, based on their roles. For example admins go to one page, while regular users go to another one. This can easily be done with a condition in the Browser component and Go To action.

You already know how to log users in and redirect them after log in, so we won't show this again.

Getting user roles

We need to get the logged user role, right after the user logs in. In our case we store the roles in the same table where our users are, but the logic is the same even if you store roles in a separate table:

Screenshot_45|616x194

What we need to is to query the table, where the roles are stored and filter it by logged user identity. The login step returns the logged user identity, so we need to add the query after it. Right click the log in step:

Screenshot_22|690x399

Then add a database query:

Screenshot_23|690x399

We call the query userdetails, then click the query builder button:

Screenshot_24|690x399

Add your table to the query builder and add the columns you need. The role column is mandatory here, as that is what we need for the redirect:

Screenshot_26|690x399

Open the Conditions tab:

Screenshot_27|690x399

Here, we will setup the query filter options, so that it returns the logged user identity. Select the user id column, select the equal condition and click the dynamic data picker to select a value:

Screenshot_28|690x399

Select the Security Login step - it returns the logged user identity:

Screenshot_34|690x399

Click Ok to create the query:

Screenshot_22|690x403

Save your server action and close the Server Connect panel:

Screenshot_31|690x399

Set up redirect

Open the App Connect Panel and select your log in form. Following the log in tutorial, you probably have a redirect to a static page already in place:

Screenshot_37|690x399

Open the dynamic action and delete what you have there:

Screenshot_38|690x399

We need to check the logged user role. In order to do this, we need to check what the query returns, so click the dynamic data picker:

Screenshot_39|690x399

Expand your log in form, open data > userdetails (the query we already created) and select role (or however is the role column named in your database table):

Screenshot_40|690x399

This will return the role, but we need to a condition here to check it. We will use the ternary operator for this. We'd like to redirect admins to certain page, and all other users to another page, so we add the following after the role value there:

== 'admin' ? 'admin.html' : 'user.html'

Which means - if the user role equals 'admin', go to 'admin.html' in all other cases go to 'user.html'. The whole expression becomes:

form1.data.userdetails[0].role == 'admin' ? 'admin.html' : 'user.html'

Screenshot_42|690x399

Click Select and you are done:

Screenshot_43|690x399

Save your page and test the results in your browser:

Screenshot_44|690x399