Using angular 9 and angular router.
I tried to follow the following thread : Angular 2 different components with same route
What I would like to achieve is to have 2 route '', one that move to privateModule if authenticated, and publicModule if not.
so basically,
if I am authenticated and go to www.myApp.com <= PrivateModule if I am not authenticated and go to www.myApp.com <= PublicModule.
I setup a app-routing.module.ts =>
const routes: Routes = [
{
matcher: publicMatcher,
loadChildren: publicModule,
},
{
matcher: privateMatcher,
loadChildren: privateModule,
canActivate: [AuthPrivateGuard],
canActivateChild: [AuthPrivateGuard],
},
]
then I have 2 matchers, and I do not know exactly how to set them up. since the url is ''
there is nothing to consume and I currently have a infinite loop.
export function privateMatcher(segments: UrlSegment[]): UrlMatchResult {
const key = localStorage.getItem('auth')
if (key && JSON.parse(key).accessToken) {
console.log('private')
return { consumed: segments }
}
return null
}
export function publicMatcher(segments: UrlSegment[]): UrlMatchResult {
const key = localStorage.getItem('auth')
if (!key || !JSON.parse(key)?.accessToken) {
console.log({ consumed: segments })
return { consumed: segments }
}
return null
}
I am probably using them wrong, or is not the good way to achieve what I want ?