0

I'm having the classic issue of my Angular 11 app not working after having made some changes, without knowing which change caused it. The app should be redirecting the user to "auth/login", but the call to router.navigate doesn't seem to do anything. I have this AuthGuardService that was also previously in use:

@Injectable()
export class AuthGuardService implements CanActivate {
 constructor(public _settings: AppSettings, public router: Router, private _authService:AuthenticationService) { }

canActivate(): boolean {

  if (!this._authService.isAuthenticated) {
    console.log('User is not authenticated!');
    if (!environment.windowsAuth) {
      console.log('Rerouting to auth/login.');
      this.router.navigate(['auth/login']);
      return false;

    } else {
      console.log('Rerouting to auth/windowslogin.');
      this.router.navigate(['auth/windowslogin']);
    }
  } else {
    console.log('Authentication check successfull.');
    return true;
  }

And here's part of the app-routing.module.ts:

  {
path: 'arbeitszeiten',
component: ArbeitszeitenOperativComponent,
canActivate: [AuthGuardService]
 },
{
 path: 'auth',
 loadChildren: () => import('./authentication.module').then(m => m.AuthenticationModule)
}];

And here's some code from the authInterceptor:

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    if (!this._authService.authorization) {
        if (req.url.endsWith(this._settings.authLoginApi) ||
            req.url.endsWith(this._settings.authWindowsloginApi) ||
            req.url.endsWith(this._settings.localizationApi) ||
            req.url.endsWith(this._settings.settingsApi) || 
            req.url.indexOf("microsoftonline") != -1) {
            return next.handle(req).pipe(tap(result => {
                
            }));
        }

The routing and part was made by a different programmer, so I'm not exactly sure what's happening there. But the console doesn't show any error, the app just never redirects to the login page. Any ideas why that might be?

Edit: after enabling the router-tracing I get the following output, I currently don't know what to make of it though: enter image description here

Maxim
  • 227
  • 2
  • 14
  • Try passing the URL as individual array elements: `this.router.navigate(['auth', 'login']);`. You can also enable the router tracing option to see what is going on. `RouterModule.forRoot( routes, { enableTracing: true } // <-- debugging purposes only )` (answered by @devqon in [this](https://stackoverflow.com/a/45669041/8712609) post ). – riorudo Mar 10 '22 at 11:44
  • thanks, the enableTracing options provides some more info that I added. Still not sure though why the navigation ends. – Maxim Mar 10 '22 at 12:03
  • What is the routing configuration for the `AuthenticationModule`, especially the one for the `login` component? – riorudo Mar 11 '22 at 13:16

0 Answers0