Base solution for your next web application
Open Closed

Conditional redirect after successful login #12453


User avatar
0
csona created

Hi Team,

I am using Aspnet zero 11.2.1 version with Angular + .Net core.

When the tenant user logs in, depending on certain feature values and tenant-specific information, I want to redirect the user to a different URL or to the dashboard.

But afterwards whenever I click on the "Dashboard" menu from left hand side, it should redirect to the dashboard page.

Could you please suggest the most appropriate place in the code to manage this logic?

Thanks

Markdown is supported
Copy & paste or drag & drop images (max 30 MB per image)

4 Answer(s)
  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi @csona

    You can manage post-login redirection inside the redirectToLoginResult method in the login.service.ts file. Currently, this method uses location.href, which causes a full page reload.

    If your goal is to redirect the user to a custom page only under specific conditions, this method is a suitable place to implement that logic. However, to preserve the single-page application (SPA) behavior of Angular, we recommend using Angular's Router instead of location.href.

    For example:

    private redirectToLoginResult(redirectUrl?: string): void {
        if (this._appSessionService.tenantId) { //This logic
            this._router.navigate(['/custom-page']);
            return;
        }
    
        if (redirectUrl) {
            this._router.navigateByUrl(redirectUrl);
        } else {
            let initialUrl = UrlHelper.initialUrl;
    
            if (initialUrl.indexOf('/account') > 0) {
                initialUrl = '/app/main/dashboard';
            }
    
            this._router.navigateByUrl(initialUrl);
        }
    }
    
    • You can handle tenant/feature-based redirection immediately after login.
    • Sidebar navigation like the "Dashboard" menu continues to work as expected, since it uses routerLink.
    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    csona created

    Hi @oguzhanagir

    I have tried as per your sample code but it actually redirects to the login page.

    And if I used location.href then it actually redirects to the normal Dashboard page i.e app/main/dashboard.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi @csona

    Yes, you are right this was my mistake; I had skipped the AuthGuard redirection. The default settings in the login.service.ts file can remain as they are, and the redirectUri parameter can optionally be omitted. If you want to redirect the user to a different page based on certain conditions after login, you need to modify the returned URL in the selectBestRoute method inside the auth-route-guard.ts file. Here, you can add a redirection URL based on your specific condition.

    selectBestRoute(): string {
            if (your_condition) {
                return '/app/admin/users';
            }
            //...
        }
    }
    
    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    csona created

    Hi @oguzhanagir ,

    Changes in SelectBestRoute method is not working,

    I have made the changes in redirectToLoginResult() method, and added the route in root-routing,module.ts file. And this change worked for me.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)