0

As angular routers need static paths, I am using canActive to dynamically route my app.

my main routing is as follows.

import { NgModule } from "@angular/core";
import { RouterModule } from "@angular/router";
import { AuthGuard, AuthData} from "./shared/auth-guard";


// DOC https://toddmotto.com/angular-component-router

@NgModule({
    imports: [
        RouterModule.forRoot([
            { path: '', redirectTo: 'app', pathMatch: 'full' },
            { path: 'auths', loadChildren: './authentification/lang.select.module#LangSelectModule' },
            { path: 'app', loadChildren: './app/app.module#AppModule', canActivate: [AuthGuard], data: { authorization: { allow: ["ADMIN", "USER"] } } },
            { path: '**', redirectTo: 'auths'} // redirect route 404

        ]) // , { enableTracing: true }) // debug only
    ],
    exports: [RouterModule],
    declarations: []
})
export class RootRoutingModule { }

The lang.select.module is

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LangChoice } from '../../app/shared/lang-choice';
const routes: Routes =
    [
        { path: '', redirectTo: 'lan', pathMatch: 'full' },
        { path: 'lan', loadChildren: './authentification.module#AuthentificationModule', canActivate: [LangChoice] },
    ]
@NgModule({
    declarations: [],
    imports: [
        RouterModule.forChild(routes)
    ]
})
export class LangSelectModule {

}

with lang-choice.ts (in which I want to navigate to URI/en/

import { Injectable } from '@angular/core';
import { ActivatedRoute, ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
import { ReplaySubject } from 'rxjs';
import defaultLanguage from './defaultLang';

@Injectable({ providedIn: 'root' })
export class LangChoice implements CanActivate {

    constructor(private router: Router, private _route: ActivatedRoute) {
    }


    canActivate(route: ActivatedRouteSnapshot,
        state: RouterStateSnapshot): ReplaySubject<boolean> {


        let subject: ReplaySubject<boolean> = new ReplaySubject<boolean>(1);
        const defaultLang = defaultLanguage();
        const routed = this.router.navigate(['/' + defaultLang], { relativeTo: this._route });

        subject.next(true);
        return subject;
    }
}

and ends in /Login component as my authentification.module.ts is as follows.

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

const routes: Routes =
    [{ path: ':langCode', redirectTo: ':langCode/login', pathMatch: 'full' },
        { path: ':langCode/login', component: LoginComponent },]

@NgModule({
    declarations: [LoginComponent,],
    imports: [  RouterModule.forChild(routes)]
})
export class AuthentificationModule {
}

I am getting the correct result for the function 'defaultLanguage' and expect to navigate to 'localhost:port/auths/lan/en/login but it seems the 'authentification.module' module is not loaded but ended in an endless loop.

any idea why?

Edayan
  • 537
  • 1
  • 7
  • 17
  • I don't know if this is your only problem but observables passed to the router have to complete. Your ReplaySubject doesn't complete. Remove the ReplaySubject in `canActive` and just return `of(true)`. – frido Sep 25 '20 at 11:47
  • @fridoo I'd argue that is not true, under the hood the guards are subscribed with something like `guard$.pipe(first())`. What you are saying might be the case for resolvers, though. – Andrei Gătej Sep 28 '20 at 21:07
  • @AndreiGătej Ok, there seems to be an inconsistency in how some guards behave when returning an Observable. The [tutorial](https://angular.io/guide/router-tutorial-toh#milestone-5-route-guards) about guards still says 'The observable provided to the Router must also complete. If the observable does not complete, the navigation does not continue.' but this isn't true for all guards. There's a github issue: https://github.com/angular/angular/issues/27656 – frido Sep 29 '20 at 16:35
  • @fridoo thanks for pointing that out – Andrei Gătej Sep 29 '20 at 16:37

0 Answers0