For some strange reason, only for certain routes, Angular Router is redirecting the user back to the base url.
Sounds like a problem with my app-routing.module.ts right?
Well this is where the strange issues start.
Strange Issue #1
Only happens when trying to navigate to /leaderboard and to /achievements but not on /team-view
Strange Issue #2
If I make a change to the code OR simply refresh the page, the problems are now resolved and I can now reach both /leaderboard and /achievements without any problems. If I log out and log back in, the problem returns.
Strange Issue #3
There are no console errors or any form of error aside from the page just flickering very quickly and redirecting me back to the / route (which is the Dashboard Component loads`)
Routes:
const routes: Routes = [
{
path: '',
pathMatch: 'full',
component: DashboardComponent,
canActivate: [LoggedInGuard],
},
{
path: 'team-view',
component: TeamViewComponent,
canActivate: [LoggedInGuard],
},
{
path: 'settings',
component: SettingsComponent,
canActivate: [LoggedInGuard],
},
{
path: 'leaderboard',
component: LeaderboardComponent,
canActivate: [LoggedInGuard],
},
{
path: 'achievements',
component: AchievementsComponent,
canActivate: [LoggedInGuard],
},
{ path: 'register', component: RegisterComponent },
{ path: 'login', component: LoginComponent },
];
Header Code:
<div class="menu-wrapper desktop-menu">
<span class="menu-item" routerLinkActive="menu-item-active" [routerLinkActiveOptions]="{ exact: true }" routerLink="/">Dashboard</span>
<span class="menu-item" routerLinkActive="menu-item-active" [routerLinkActiveOptions]="{ exact: true }" routerLink="/leaderboard">Leaderboard</span>
<span class="menu-item" routerLinkActive="menu-item-active" [routerLinkActiveOptions]="{ exact: true }" routerLink="/achievements">Achievements</span>
<span class="menu-item" routerLinkActive="menu-item-active" [routerLinkActiveOptions]="{ exact: true }" routerLink="/team-view">My Team</span>
<span class="menu-item" routerLinkActive="menu-item-active" (click)="ToggleNotifications()"><mat-icon matBadge="4">notifications</mat-icon></span>
<span class="menu-item" routerLinkActive="menu-item-active" (click)="SignOut()"><mat-icon>logout</mat-icon></span>
</div>
LoggedInGuard:
@Injectable()
export class LoggedInGuard implements CanActivate {
constructor(private router: Router) {}
canActivate(): Observable<boolean> | Promise<boolean> | boolean {
const isLoggedIn = localStorage.getItem('jwt') !== null;
if (!isLoggedIn) {
console.log('Returning to login screen');
this.router.navigateByUrl('login');
}
return isLoggedIn;
}
}
I also noticed some weird logging in the Angular Routing that seems to indicate that Angular Router is forcing a redirect but I don't know why?:
GuardsCheckEnd(id: 11, url: '/', urlAfterRedirects: '/', state: Route(url:'', path:'') { Route(url:'', path:'') } , shouldActivate: true)