Members Login Redirect Problem

I am currently using the following code for member login. Since there are multiple types of members and roles, the member is intended to be redirected to a specific page based on their type or role after login. However, I am encountering an issue where, according to the current code, the member is briefly redirected to the homepage for a few seconds before being redirected to the assigned page. If anyone has any insights or solutions to resolve this issue, I would greatly appreciate your assistance.

Frontend Code:

import wixLocation from "wix-location";
import { checkAccountNumber } from "backend/verifyMemberAccount";
import { authentication, currentMember } from "wix-members-frontend";
import { closeLightbox } from "public/utils.js";

$w.onReady(async function () {
    $w("#errorText").text = "Your account number was sent to your email during the registration process. If you haven’t received it, please check your spam folder."
   

    // Listen for input in the 'soloistAccountNumber' field
    $w('#soloistAccountNumber').onInput(async () => {
        // Get the account number from the input field
        let accountNumber = $w('#soloistAccountNumber').value;

        // If the account number is empty, hide the accountType element
        if (!accountNumber) {
            $w("#accountTypeButton").hide();
            return; // Exit the function early
        }

        // Call the backend function to check if the account number exists
        try {
            const response = await checkAccountNumber(accountNumber);

            if (response.exists) {
                // Store the customType value for later use
                

                // Show the accountType text element and set its text
                $w("#accountTypeButton").label = response.customType + " Account";
                $w("#accountTypeButton").show(); // Make the element visible
                $w("#soloistEmailInputBox").expand();
                $w("#accountVerificationButton").expand();
            } else {
                // Hide the accountType element if the account number doesn't exist
                $w("#accountTypeButton").hide();
                $w("#soloistEmailInputBox").collapse();
                $w("#accountVerificationButton").collapse();
            }
        } catch (error) {
            console.error("Error:", error);
            $w("#accountTypeButton").hide(); // Hide the accountType element in case of an error
        }
    });

    // Handle the account verification button click
    $w("#accountVerificationButton").onClick(async () => {
        // Get the values from the input fields
        let accountNumber = $w('#soloistAccountNumber').value;
        const email = $w("#soloistEmailInput").value;

        // Validate input fields
        if (!accountNumber && !email) {
            // Both fields are empty
            $w('#errorText').text = "Please enter your account number and email.";
            return; // Exit the function early
        } else if (!accountNumber) {
            // Account number is empty
            $w('#errorText').text = "Please enter your account number.";
            return; // Exit the function early
        } else if (!email) {
            // Email is empty
            $w('#errorText').text = "Please enter your email.";
            return; // Exit the function early
        }

        // Call the backend function to check if the account number exists
        try {
            const response = await checkAccountNumber(accountNumber);

            if (response.exists) {
                // Check if the loginEmail matches the email entered in soloistEmailInput
                if (response.loginEmail === email) {
                    // Change the state of the multi-state box to 'soloistSecondStep'
                    $w('#soloistLoginStateBox').changeState('soloistSecondStep');
                    // Collapse the error text if it was expanded
                    $w("#errorText").text = "Your account number was sent to your email during the registration process. If you haven’t received it, please check your spam folder."
                } else {
                    // Expand the error text to show the error message
                    $w('#errorText').text = "The email does not match the account number.";
                }
            } else {
                // Expand the error text to show the error message
                $w('#errorText').text = "The account number does not exist.";
            }
        } catch (error) {
            console.error("Error:", error);
            $w('#errorText').text = "An error occurred. Please try again.";
        }
    });

    // Login

    $w("#soloistaccountloginButton").onClick(async () => {
        const email = $w("#soloistEmailInput").value;
        const password = $w("#soloistPasswordInput").value;

        try {
            // 1. Login
            await authentication.login(email, password);

            // 2. Get roles
            const roles = await currentMember.getRoles();
            const roleIds = roles.map(role => role._id);

            // 3. Redirect based on role ID
            if (roleIds.includes("c071715d-9431-451b-8b2a-335600e4402e")) {
                wixLocation.to("/dashboard/soloist");
            } else if (roleIds.includes("58d8cb69-2580-497d-8b57-051908ab04eb")) {
                wixLocation.to("/dashboard/enterprise");
            }
        } catch (error) {
            // Simple error message
            $w("#passwordErrorText").text =
                error.details?.applicationError?.code === "-19976" ?
                "Invalid email/password" :
                "Login failed. Try again later.";

            $w("#passwordErrorText").expand();
        }
    });

    // Back button logic
    $w("#soloistBackButton").onClick(() => {
        $w('#soloistLoginStateBox').changeState('soloistFirstStep');
    });

    // Reset error state when input fields are changed
    $w("#soloistAccountNumber").onFocus(() => {
        $w("#errorText").text = "Your account number was sent to your email during the registration process. If you haven’t received it, please check your spam folder."
    });

    $w("#soloistEmailInput").onFocus(() => {
        $w("#errorText").text = "Your account number was sent to your email during the registration process. If you haven’t received it, please check your spam folder."
    });

    $w("#soloistPasswordInput").onFocus(() => {
        $w("#passwordErrorText").collapse();
    });

    $w("#closeLoginButton").onClick(() => {
        closeLightbox();
    });
});

Backend Code:

import { contacts } from "wix-crm-backend";


export async function checkAccountNumber(accountNumber) {
    try {

        // Query all contacts
        const queryResults = await contacts.queryContacts().find({ suppressAuth: true });

        // Filter contacts to find those with the matching custom.account field
        const matchingContacts = queryResults.items.filter((contact) => {
            // Check if the custom.account field exists and matches the provided accountNumber
            return (
                contact.info.extendedFields &&
                contact.info.extendedFields["custom.account"] === accountNumber
            );
        });

        // If a matching contact is found, return its custom.type field
        if (matchingContacts.length > 0) {
            const matchingContact = matchingContacts[0]; // Get the first matching contact
            const customType = matchingContact.info.extendedFields["custom.type"]; // Get the custom.type field
            const loginEmail = matchingContact.primaryInfo.email;

            return {
                exists: true,
                customType: customType || null, // Return custom.type or null if it doesn't exist
                loginEmail: loginEmail || null
            };
        } else {
            // If no matching contact is found, return exists: false
            return { exists: false };
        }
    } catch (error) {
        console.error("Error querying contacts:", error);
        return { exists: false };
    }
}




Thanks in advance.