Looking at my route setup, it's structured like this:
const routes: Routes = [
{ path: '', redirectTo: '/login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard] },
];
On top of that, there is an AuthGuard
class:
export class AuthGuard implements CanActivate, CanActivateChild {
constructor(private router: Router,
private authService: AuthService) { }
canActivate() {
if (this.authService.isLoggedIn()) {
return true;
}
this.router.navigate(['login']);
return false;
}
}
The current behavior redirects the user to the login page upon visiting the site and prevents access to /dashboard
without authentication. The objective now is to redirect a logged-in user directly to /dashboard
. For instance, when logging in at myapp.com
, I aim to be instantly directed to myapp.com/dashboard
.