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
4 Answer(s)
-
0
Hi @csona
You can manage post-login redirection inside the
redirectToLoginResultmethod in thelogin.service.tsfile. Currently, this method useslocation.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
Routerinstead oflocation.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 supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
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 supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
Hi @csona
Yes, you are right this was my mistake; I had skipped the
AuthGuardredirection. The default settings in thelogin.service.tsfile can remain as they are, and theredirectUriparameter 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 theselectBestRoutemethod inside theauth-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 supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
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 supportedCopy & paste or drag & drop images (max 30 MB per image)