I created a custom validation class to verify if a user is logged in before allowing them access to a specific page. However, after implementing this validation, my program no longer routes me to the intended component.
Validation.ts
export class UserValidation {
user = {};
validateLogin(user) {
if (user) {
this.user = user;
console.log("test login");
return true;
} else {
this.user = {};
console.log("test logout");
return false;
}
}
}
app.component.ts
export class AppComponent {
...
login(email: any, password: any) {
this.af.auth.login({ email: email, password: password }, {
provider: AuthProviders.Password,
method: AuthMethods.Password
});
this.modal.close();
this.router.navigate(['dashboard']);
}
}
dashboard.component.ts
export class DashboardComponent {
constructor(public af: AngularFire, public router: Router) {
this.validator = new UserValidation();
this.af.auth.subscribe(user => {
if (this.validator.validateLogin(user)) {
this.af.auth.subscribe(auth => {
...
});
}
else {
this.router.navigate(['/']);
}
});
}
}
When I run the program and start at '/', it functions correctly. However, when I attempt to log in, the console displays "test logout" first, preventing the routing to the dashboard. Any suggestions on how to speed up the validation process or ensure it occurs after the login?
test logout
test login