CanActivate Getting apply to all route after navigated to Home route


#1

I am using lazyloading for my nativescript application

I have routes like this

export const routes: Routes = [
{
path: ‘’,
redirectTo: ‘/home’,
pathMatch: ‘full’
},
{
path: ‘login’,
loadChildren: ‘./login/login.module#LoginModule’
},
{
path: ‘home’,
loadChildren: ‘./home/home.module#HomeModule’,
canActivate: [NeedsAuthentication]
}
];

This are login routes for login modules
export const routes: Routes = [
{
path: ‘’,
component: LoginComponent
},
{
path: ‘login-with-otp’,
component: LoginWithOtpComponent
},
{
path: ‘login-with-email’,
component: LoginWithEmailComponent
},
{
path: ‘login-two-way’,
component: LoginTwoWayComponent
},
{
path: ‘forgot-password’,
component: ForgotComponent
},
{
path: ‘signup’,
component: SignUpComponent
}
];

This are home routes for homemodules
export const routes: Routes = [
{
path: ‘’,
component: HomeComponent
},
];

Initially everythings work fine. But after login , when i logout from home modules…… my CanActivate gaurd also get applied on Login modules

This is my Guard
@Injectable()
export class NeedsAuthentication implements CanActivate {
constructor(private _router: Router, private store: Store, private routerExtensions: Router, private _loginActions: LoginActions) {
}

public canActivate() {
return this.store.select(s => s.session).select(session => {
if (session && session.user) {
console.log(‘please allow me’);
return true;
} else {
console.log(‘please login first’);
this._router.navigate([’/login’], { replaceUrl: true });
return false;
}
});
}
}


#2

you can check here that initially needsAuthentication is not called by after login and i do logout … needsAuthentication gaurd get call for all routes