当前,我登录我的用户,然后要实现一些路由防护,以使如果用户未登录则无法访问它们。
所以这是我的警卫。
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Observable } from 'rxjs';
import { AngularFireAuth } from '@angular/fire/auth';
@Injectable({
providedIn: 'root'
})
export class AuthguardGuard implements CanActivate {
constructor(public af: AngularFireAuth){
}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
var user = this.af.auth().currentUser;
if (user) {
return true;
} else {
return false;
}
}
}
问题是---> var user = this.af.auth().currentUser;
出现错误
Cannot invoke an expression whose type lacks a call signature. Type 'Auth' has no compatible call signatures.ts(2349)
(property) AngularFireAuth.auth: firebase.auth.Auth
oO我应该如何检查用户是否已登录?我不想为此使用localSTorage。
答案 0 :(得分:0)
我最终使用了以下canActivate
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.auth.authState.pipe(map(User => {
if (User) {
return true;
} else {
this.router.navigate(['/login']);
return false;
}
}));
}