Reputation: 701
so far, for differenciate the pages I had to play around whit the children route, basically if the user is logged the MainComponent
has to be loaded because in that there is the top bar.
Instead when the user goes to login
the top bar is not loaded.
I'd like that when the user is logged and he goes to \
the page loaded would be the \dashboard
and instead when he is not and he goes to \
the page loaded would be the \login
page.
I tried to use the authguard
but every time that I'm adding canActivate: [AuthGuard]
my site doesn't work anymore.
my file app-routing.module.ts is :
const routes: Routes = [
{
path: '',
component: MainComponent,
children: [
{ path: '', component: DashboardComponent },
{ path: 'missions', component: MissionComponent },
{ path: 'operators', component: OperatorComponent },
{ path: 'materials', component: MaterialComponent },
{ path: 'bins', component: BinComponent },
{ path: 'main', component: MainComponent },
]
},
{ path: 'login', component: LoginComponent },
];
my auth.guard.ts is :
constructor(
public afAuth: AngularFireAuth,
public userService: UserService,
private router: Router
) {}
canActivate(): Promise<boolean> {
return new Promise((resolve, reject) => {
this.userService.getCurrentUser()
.then(user => {
//this.router.navigate(['/operator']);
return resolve(false);
}, err => {
return resolve(true);
});
});
}
Upvotes: 0
Views: 161
Reputation: 9334
Try this:
canActivate() {
return this.userService.getCurrentUser()
.pipe(
tap(user => {
if (!user) {
this.router.navigate(['/operator']);
}
}),
);
}
If the user is returned, it means it is logged in and if not then it will be redirected where ever you want to redirect.
Upvotes: 1