czytałem instrukcji tutaj: https://angular.io/docs/ts/latest/guide/router.htmlAuthGuard nie czekać do uwierzytelniania, aby zakończyć przed sprawdzeniem użytkownik
wydaje się dość prosta, jednak nie jestem pewien, w jaki sposób korzystać z uwierzytelniania angularfire2 w ramach straży auth (canActivate
). Co Próbowałem to:
AuthService
import { Injectable } from '@angular/core';
import { AngularFire } from 'angularfire2';
@Injectable()
export class AuthService {
private user: any;
constructor(private af: AngularFire) {
this.af.auth.subscribe(user => {
this.user = user;
})
}
get authenticated(): boolean {
return this.user ? true : false;
}
}
AuthGuard
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router, private authService: AuthService) { }
canActivate(): Observable<boolean> | boolean {
if (this.authService.authenticated)
return true;
this.router.navigate(['/login']);
return false;
}
}
Dodałem także AuthService bootstrap dostawców. Ten sposób działa dobrze, jednak moim głównym problemem jest to, że odświeżam (lub początkowo ładuję) stronę, która ma AuthGuard
, zawsze przekierowuje mnie do strony logowania, ponieważ AuthGuard
nie czeka na odpowiedź uwierzytelnienia. Czy istnieje sposób oczekiwania na zakończenie uwierzytelniania (nawet jeśli się nie udało) i następnie sprawdzić, czy użytkownik jest uwierzytelniony?
Zobacz tę odpowiedź, że wdrożył straży auth prawidłowo http://stackoverflow.com/a/37889258/652850 –
naprawdę nie rozwiązać mój problem z angularfire2. – Andrew