Ionic framework 认证+;爱奥尼亚+;Firestore&x2B;防护(canActivate)-访问权限
我正在用Ionic创建一个应用程序,它使用Firebase对用户进行身份验证。一旦成功执行身份验证,将其数据保存在Firestore中。用户Firestore集合中的一项是“activeUser:Boolean”,我可以在登录后通过“AngularFirestore”插件返回的可观察(用户$)进行监控 我创建了一个canActivate类型的保护文件,以便在Firestore中的“activeUser”标志变为“False”并且用户尝试导航到另一个页面时立即监视该标志,该标志将被“注销”,并通知其用户已被管理员停用 我对爱奥尼亚是个新手,有些学科,比如卫兵和观察者,我有疑问。我希望你能帮助我,如果下面的代码是合理的,或者有一些我们可以改进的地方 另一个问题是关于可观测的。我可以在应用程序中需要时订阅observable USER$吗?我应该注意表现吗?我在哪里退订爱奥尼亚号 如果有人有使用Guard和Firestore的代码示例或一些教程链接,我将不胜感激 谢谢大家的帮助 authentication.Service.tsIonic framework 认证+;爱奥尼亚+;Firestore&x2B;防护(canActivate)-访问权限,ionic-framework,firebase-authentication,angular-router-guards,Ionic Framework,Firebase Authentication,Angular Router Guards,我正在用Ionic创建一个应用程序,它使用Firebase对用户进行身份验证。一旦成功执行身份验证,将其数据保存在Firestore中。用户Firestore集合中的一项是“activeUser:Boolean”,我可以在登录后通过“AngularFirestore”插件返回的可观察(用户$)进行监控 我创建了一个canActivate类型的保护文件,以便在Firestore中的“activeUser”标志变为“False”并且用户尝试导航到另一个页面时立即监视该标志,该标志将被“注销”,并通知
/**
* Funcao que fica 'ouvindo' o estado da autenticado
* e obtem os dados armazenados no firestore referente
* ao usuario logado.
**/
this.user$ = this.afAuth.authState.pipe(
switchMap(user => {
if (user) {
return this.afStore.doc<IUser>(`users/${user.uid}`).valueChanges();
} else {
return of(null);
}
})
);
export class UserDisabledGuard implements CanActivate {
constructor(private authService: AuthenticationService, private router: Router) { }
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> {
return this.authService.user$.pipe(
filter(val => val !== null),
take(1),
map(response => {
if (response.activeUser) {
this.authService.logout();
Storage.remove({ key: TOKEN_KEY });
this.authService.isAuthenticated.next(false);
this.router.navigateByUrl('/login');
return false;
} else {
return true;
}
})
);
}
}
/**
*“乌文多”和“奥特蒂卡多”功能
*无火商店参考号
*奥乌萨里奥·洛加多。
**/
this.user$=this.afAuth.authState.pipe(
开关映射(用户=>{
如果(用户){
返回这个.afStore.doc(`users/${user.uid}`)。valueChanges();
}否则{
返回(空);
}
})
);
用户活动.guard.ts
/**
* Funcao que fica 'ouvindo' o estado da autenticado
* e obtem os dados armazenados no firestore referente
* ao usuario logado.
**/
this.user$ = this.afAuth.authState.pipe(
switchMap(user => {
if (user) {
return this.afStore.doc<IUser>(`users/${user.uid}`).valueChanges();
} else {
return of(null);
}
})
);
export class UserDisabledGuard implements CanActivate {
constructor(private authService: AuthenticationService, private router: Router) { }
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> {
return this.authService.user$.pipe(
filter(val => val !== null),
take(1),
map(response => {
if (response.activeUser) {
this.authService.logout();
Storage.remove({ key: TOKEN_KEY });
this.authService.isAuthenticated.next(false);
this.router.navigateByUrl('/login');
return false;
} else {
return true;
}
})
);
}
}
导出类UserDisabledGuard实现CanActivate{
构造函数(私有authService:AuthenticationService,私有路由器:路由器){}
激活(
路由:ActivatedRouteSnapshot,
状态:RouterStateSnapshot):可观察{
返回此.authService.user$.pipe(
过滤器(val=>val!==null),
以(1)为例,
映射(响应=>{
if(response.activeUser){
this.authService.logout();
删除({key:TOKEN\u key});
this.authService.isAuthenticated.next(false);
this.router.navigateByUrl('/login');
返回false;
}否则{
返回true;
}
})
);
}
}