I’m working on a web application using Angular and i came across a problem.
There is an Authentication service that logs in a user. I’m sending a request to
the server with the credentials and waiting for a response. The problem is that I
try to navigate from the login component to the home component In the subscription
of the responded
login(formValues) {
this.auth.logInUser(formValues.username, formValues.password)
.subscribe(response =>{
if(!response){
this.invalidLogin=true;
} else {
this.router.navigate(['stream']);
}
})
}
But every other component has a canActivateGuard that checks whether the current user is logged in (the data that i'm waiting from the server).
export const appRoutes: Routes = [
{path: 'login', resolve: LiveStreamResolver, component: LoginComponent},
{path: 'reports', component: ReportsComponent, resolve: LiveStreamResolver, canActivate: [AuthGuardService]},
{path: 'calendar', component: CalendarComponent, resolve: LiveStreamResolver, canActivate: [AuthGuardService]},
{path: 'stream', component: LiveStreamComponent},
{path: '', redirectTo: 'login', pathMatch: 'full'}
];
constructor(public auth: AuthenticationService) { }
canActivate(): boolean {
return !!this.auth.currUser;
}
Is there a way to resolve before the canActivate check is done? Is there any others solutions maybe?
Any other advises for how to guard the components would be welcomed :D