Redirect based on member's role

Question:
Good Morning :slight_smile:. Had a project where user should be redirected to a specific page based on its role on login. Developed a small code which may help you as well. Feel free to use this code and adjust per your requirements, it was created fast so it may be improved :slight_smile:

To work properly, should be placed in masterPage.js

import wixLocationFrontend from 'wix-location-frontend';
import { authentication, currentMember } from 'wix-members-frontend';

$w.onReady(function () {
    // Listen for login events
    authentication.onLogin(() => {
        // Retrieve the roles of the logged-in member
        currentMember.getRoles()
            .then((roles) => {
                if (roles.length > 0) {
                    let firstRoleTitle = roles[0].title;

                       // Redirect the member to different pages based on their first role
                    if (firstRoleTitle === "Role1") {
                        wixLocationFrontend.to("/page1"); // Redirect to Page1 for Role1
                    } else if (firstRoleTitle === "Role2") {
                        wixLocationFrontend.to("/page2"); // Redirect to Page2 for Role2
                    }
                }
            })
            .catch((error) => {
                console.error("Error retrieving member roles: ", error);
            });
    });
});

Just a note for anyone that IS looking to redirect a user after login ……

I usually don’t recommend to clients that they do this.

Any Paid Plans subscription via Wix Stores, Bookings, Paid Plans, Wix Events, etc requires a user to be a member and be logged in before continuing with the sale / transaction. Across native Wix apps, there are also native prompts that encourage users to create an account, such as Wix Stores, Wix Blog, Wix Forum, etc.

If you create a redirect upon Login, then it does this for ALL members, including those that are in the middle of a transaction. Breaking the natural flow of the sale will hurt conversions. It also includes breaking the flow of users trying to go to a Forum post or Blog post to continue reading, etc. Again, it just breaks the natural flow of their experience.

The reason a website Owner MIGHT want to innately navigate a user is most likely to make it “easier” for the user to get to where they are going based on who they are.

By experience, it is a better approach to have a custom navigation bar / buttons for different roles instead of forcing an immediate redirect upon logging in. Let the user continue their own navigation, simply present better navigation options.

It depends on factors. The reason of this code is already making something better for the UX part like in the native apps you are referencing.

If you code it right it won’t break anything. It’s also important which users will be redirected? (The role you’ll be checking may be temp role to rate-limit users etc.)

It says based on role not for all members. Maybe you’ll create a system where you’ll mark users based on a role and then you’ll redirect them to actually where they want to go.

Routers are working in that way they are checking some conditions and redirecting you to a page.

So when you design it right you won’t have any problems or conflicts with native Wix Apps in fact you’ll make UX better.

2 Likes

This is true, not everyone will have a role.

But the majority of the native apps do not have a URL or an API to get you “back to where you were” when you successfully log in due to the internal native Windows for the different steps inside each app.

The simplicity of “log in now redirect “ will break more UX than help it.

An advanced coder with experience in complex logic flow will be someone most likely to successfully implement a better UX (but because there will probably be a whole lot of coding to replace different native entry points and potential scenarios when multiple native Wix apps are involved.)