angular - 无法检查用户是否已登录 Firebase + Angular
问题描述
目前我登录了我的用户,然后我想实现一些路由守卫,以便在用户未登录时无法访问它们。
所以这是我的后卫。
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。
解决方案
我最终使用了以下 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;
}
}));
}
推荐阅读
- python - 如何根据列表中相同的设置值对列表列表进行排序?
- java - 寻找最好的方法来做一个 nato phoetic 字母转换器
- sql - 将多行转换为具有更多列的单行
- julia - 如何获取给定时区的时区偏移量
- python - `_create_slots(self, var_list)` 只是未来扩展的占位符吗?
- wso2 - WSO2 Identity Server CAS 是否支持续订选项?
- reactjs - 在 rc-yearly-calendar 上反应引导工具提示
- node.js - 使用上下文 Api 对用户进行身份验证
- javascript - 客户端验证然后传递到服务器或禁用提交直到表单完成 - 我该怎么做?
- elasticsearch - etup 弹性网络流模块