Redirection after successful login based on user level ID (v2024)

Hi,
I want to redirect each user (after successful login) based on his/her user level ID.
I added the follwing code to the “login page /Page_Redirecting” server event but it NOT works:

// دریافت سطح دسترسی کاربر
    $userLevel = CurrentUserLevel(); 
    $profile = Profile()->loadFromSession();
    $SupportStaff = $profile ? $profile->get("SupportStaff") : null;

    // تعیین مسیر هدایت
    switch (true) {
        case ($userLevel === -1): 
            $url = "/appointmentslist?cmd=resetall";
            break;
        case ($userLevel === 1):
            $url = "/doctorpanel";
            break;
        case ($userLevel === 4):
            $url = "/receptionpanel";
            break;
        case ($SupportStaff === 'Y'):
            $url = "/clinicpanel"; // اصلاح نام مسیر (در صورت تایپی بودن)
            break;
        default:
            $url = "/home";
            break;
    }

How to do this?
Thanks

Shouldn’t you use redirection command?

$this->terminate($url);

There are a few problems in your code:

  1. You load profile from session, but did you save to session before? If not, Profile() is fine.
  2. You get “SupportStaff” from profile, but did you set it before? If not, you cannot. If “SupportStaff” is only a field from the users table, you may simply use CurrentUserInfo("SupportStaff").
  3. You should check if the user is logged in first (e.g. by IsLoggedIn(), otherwise you code will always redirect to “/home” if the user is not logged in yet.

Thanks.
I changed the code but the problem exists:

    // Page Redirecting event
    public function pageRedirecting(&$url)
    {
        // Example:
        //$url = "your URL";	
        $userLevel = CurrentUserLevel(); 
        switch (true) {
        case (IsLoggedIn() && $userLevel === -1): 
            $url = "/appointmentslist?cmd=resetall";
            break;
        case (IsLoggedIn() && $userLevel === 1):
            $url = "/doctorpanel";
            break;
        case (IsLoggedIn() && $userLevel === 4):
            $url = "/receptionpanel";
            break;
        case (IsLoggedIn() && $userLevel === 4 && CurrentUserInfo("SupportStaff") === 'Y'):
            $url = "/clinicpanel"; 
            break;
    }
    }

You need to explain “not work” or “problem exists”.

  1. What did you see? Any error message?
  2. Did you enable Debug? Any error message in the log file?
  3. Did you check Network panel in your browser? What is the response, any redirection URL? As always, you should check HTTP response.
  4. You may also add log to debug your code, e.g.
Log("Login - Page_Redirecting", [$userLevel, CurrentUserLevel(), IsLoggedIn(), CurrentUserInfo("SupportStaff")]);

See the result in log file, check if any of the cases in your code is met.

There is NO any error. I enabled Debug mode:

[2025-01-30T17:48:01.752048+03:30] log.DEBUG: Login - Page_Redirecting [-2,-2,false,null]

As suggested: