简体   繁体   English

错误 TS2345 Angular AngularFire2 检查用户是否为管理员

[英]error TS2345 Angular AngularFire2 checking if user isAdmin

Trying to use the latest version of Firebase and Angular.尝试使用最新版本的 Firebase 和 Angular。 Overall concept I am trying to achieve is admin authentication.我试图实现的总体概念是管理员身份验证。 I have achieved login logout functions.我已经实现了登录注销功能。 I have saved the googleAuth login user to database with UID.我已使用 UID 将 googleAuth 登录用户保存到数据库。 Now what I am trying to do is restrict some pages to Logged in and Admin.现在我要做的是将某些页面限制为登录和管理员。 So I have assigned isAdmin: true as a value in the user object that is stored in the database.因此,我已将 isAdmin: true 指定为存储在数据库中的用户对象中的值。 Basically I want to see if the user is admin and return true or false for the router params.基本上我想看看用户是否是管理员并为路由器参数返回真或假。 I have been using Angular and Firebase on and off.我一直在使用 Angular 和 Firebase。 I would consider myself a beginner.我会认为自己是初学者。 I can't quite understand how to migrate from AngularFire2 4 -> AngularFire 5. The current error I am getting is TS2345:我不太明白如何从 AngularFire2 4 -> AngularFire 5 迁移。我得到的当前错误是 TS2345:

ERROR in src/app/admin-auth-guard.service.ts(15,38): error TS2345: Argument of type '(user: User) => AngularFireObject<AppUser>' is not assignable to parameter of type '(value: User, index: number) => ObservableInput<{}>'.
  Type 'AngularFireObject<AppUser>' is not assignable to type 'ObservableInput<{}>'.
    Type 'AngularFireObject<AppUser>' is not assignable to type 'ArrayLike<{}>'.
      Property 'length' is missing in type 'AngularFireObject<AppUser>'.

from what I think I understand the get function is returning an Object, but the error is expecting an observable.从我认为我理解的 get 函数返回一个对象,但错误是期待一个可观察的。 All I know about observables are that you can subscribe to them and you should destroy them...我所知道的关于 observables 是你可以订阅它们,你应该销毁它们......

I need to use currently logged in user id to query to database and return the user object matching the uid and then check to see if isAdmin is true or false.我需要使用当前登录的用户 id 查询数据库并返回与 uid 匹配的用户对象,然后检查 isAdmin 是真还是假。 Then return that to the route params so I can restrict pages to Authenticated and Admin.然后将其返回到路由参数,以便我可以将页面限制为 Authenticated 和 Admin。

Please point me in the right direction.请指出我正确的方向。

admin-auth-guard.service.ts admin-auth-guard.service.ts

import { Injectable } from '@angular/core';
import { CanActivate } from '@angular/router';
import { AuthService } from './auth.service';
import { UserService } from './user.service';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class AdminAuthGuardService implements CanActivate {

  constructor(private auth: AuthService, private userService: UserService) { }

  canActivate(): Observable<boolean> {
    return this.auth.user$.switchMap(user => this.userService.get(user.uid)).map(appUser => appUser.isAdmin);
  }

}

user.service.ts用户服务.ts

import { Injectable } from '@angular/core';
import { AngularFireDatabase, AngularFireObject } from 'angularfire2/database';
import * as firebase from 'firebase';
import { Observable } from 'rxjs/Observable';
import { AppUser } from './models/app-user';

@Injectable()
export class UserService {

  constructor(private db: AngularFireDatabase) { }

  save(user: firebase.User) {
    this.db.object('/users/' + user.uid).update({
      name: user.displayName,
      email: user.email
    });
  }

  get(uid: string): AngularFireObject<AppUser> {
    return this.db.object('/users/' + uid);
  }

}

this is what I have been using for help: https://github.com/angular/angularfire2/blob/master/docs/version-5-upgrade.md这是我一直在使用的帮助: https : //github.com/angular/angularfire2/blob/master/docs/version-5-upgrade.md

I don't understand how the annotations work exactly for the functions created.我不明白注释对于创建的函数是如何工作的。 In reference to "observable" from this i assume means this canActivate() function extends from a Observable and should return a boolean (true/false).关于“observable”,我认为这意味着这个 canActivate() 函数从一个 Observable 扩展而来,并且应该返回一个布尔值(真/假)。

Thank you for your time.感谢您的时间。

 get(uid: string): Observable<any> {
    return this.db.object('/users/' + uid).valueChanges();
 }

And :和 :

canActivate(): Observable<boolean> {
   return this.auth.user$.switchMap(user => 
   this.userService.get(user.uid)).map((appUser :any) => appUser.isAdmin);
}

For more information see : info有关更多信息,请参阅: 信息

In my case, it works.就我而言,它有效。
AdminAuthGuard Service. AdminAuthGuard 服务。

import { Injectable } from '@angular/core';
import { CanActivate } from '@angular/router';
import { AuthService } from './auth.service';
import { map, switchMap } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { UserService } from './user.service';
import * as firebase from 'firebase';

@Injectable()
export class AdminAuthGuard implements CanActivate {

  constructor(private auth: AuthService,
  private userService: UserService) {}

  canActivate(): Observable<boolean> {
    return this.auth.user$.pipe(switchMap((user: firebase.User) =>
      this.userService.get(user.uid)).
      map((appUser) => appUser.isAdmin));
   }

}

UserService.ts用户服务.ts

 get(uid: string): Observable<any> {
    return this.db.object('/users/' + uid).valueChanges();
 }

AdminAuthGuard Service. AdminAuthGuard 服务。

 canActivate(): Observable<boolean> {
 return this.auth.user$
 .switchMap(user => this.userService.get(user.uid).valueChanges())
 .map(appUser => appUser.isAdmin);
 }    

UserService.ts用户服务.ts

get(uid:string):AngularFireObject<AppUsers>{
return this.db.object('/users/'+uid);
}
Admin-auth-guard.service.ts

canActivate(): Observable<boolean> {
     return this.auth.user$
    .pipe(switchMap(user=> {
        return this.userService.get(user.uid).valueChanges()
        }),
        map(appUser=> appUser.isAdmin));
  }


user.service.ts

get(uid: string): AngularFireObject<AppUser>{
    return this.db.object('/users/'+ uid);
  }

user.service.ts用户服务.ts

get(uid: string): Observable<any> {
return this.db.object('/users/' + uid).valueChanges();
}

admin-auth-guard.service.ts admin-auth-guard.service.ts

canActivate(): Observable<boolean> {
return this.auth.user$.pipe(switchMap(user => this.userService.get(user.uid).pipe(map((appUser: any) => appUser.isAdmin))));
}

"FirebaseObjectObservable" replace it with "Observable" and import it from rxjs "FirebaseObjectObservable" 将其替换为 "Observable" 并从 rxjs 导入

import { Observable } from 'rxjs';从 'rxjs' 导入 { Observable };

I tried above ways in angular 8 but nothing works. 我在角度8中尝试了上述方法,但没有任何效果。 Please remove get method from user service and implements as below code. 请从用户服务中删除get方法,并实现以下代码。

canActivate(): Observable<boolean>{
    return this.authService.user$.pipe(
      switchMap((user: firebase.User) => this.db.object('/user/'+ user.uid).valueChanges()),
      map((user: AppUser) =>  user.isAdmin))
}

I get this working with angular 8 only this way:我只能通过这种方式使用 angular 8:

 canActivate(): Observable<boolean> { return this.auth.user$.pipe( switchMap((user: firebase.User) => this.userService.get(user.uid) .pipe( map((appUser) => appUser.isAdmin) ) ) ) }

and UserService get method和 UserService 获取方法

 get(uid: string): Observable<any> { return this.db.object('/users/' + uid).valueChanges(); }

admin-auth-guard.service.ts admin-auth-guard.service.ts

canActivate(): Observable<boolean> {
    return this.auth.user$
    .pipe(switchMap(user=> this.userService.get(user.uid).valueChanges()),
    map(appUser=> appUser.isAdmin));
 }

user.service.ts用户服务.ts

get(uid: string): AngularFireObject<AppUser>{
  return this.db.object('/users/'+ uid);
}

it can be done like this below and could have directly mapped since both are Observables.它可以像下面这样完成并且可以直接映射,因为两者都是 Observables。 But FirebaseObjectObservable is deprecated in latest versions of angular.但是 FirebaseObjectObservable 在最新版本的 angular 中已被弃用。

get(uid: string): FirebaseObjectObservable<AppUser>{
      return this.db.object('/users/'+ uid);
    }

So when we are using AngularFireObject we have to do .valueChanges() then it is going to return an observable so that we can return a Boolean observable finally in the canActivate()因此,当我们使用 AngularFireObject 时,我们必须执行 .valueChanges() 然后它将返回一个 observable,以便我们最终可以在 canActivate() 中返回一个布尔 observable

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular AngularFire2检查用户isAdmin - Angular AngularFire2 checking if user isAdmin 角度2错误TS2345 - Angular 2 Error TS2345 Angular 11.2.6 或 Typescript TS2345 错误 - Angular 11.2.6 or Typescript TS2345 error Angular2 TypeScript指令错误TS2345 - Angular2 TypeScript Directive error TS2345 角度2 +茉莉花单元测试 - 获得错误TS2345 - Angular 2 + Jasmine Unit Test - Getting Error TS2345 Angular 错误 TS2345:“MonoTypeOperatorFunction”类型的参数<event> ' 不可分配给 'OperatorFunction 类型的参数<event, event> '</event,></event> - Angular Error TS2345: Argument of type 'MonoTypeOperatorFunction<Event>' is not assignable to parameter of type 'OperatorFunction<Event, Event>' Angular - 错误 TS2345:类型参数 'string | null' 不能分配给“字符串”类型的参数 - Angular - error TS2345: Argument of type 'string | null' is not assignable to parameter of type 'string' Angular 5错误TS2345:“数字”类型的参数无法分配给“字符串”类型的参数 - Angular 5 error TS2345: Argument of type 'number' is not assignable to parameter of type 'string' Angular2 / TypeScript:错误TS2345:类型'String'的参数不能分配给'string'类型的参数 - Angular2/TypeScript: error TS2345: Argument of type 'String' is not assignable to parameter of type 'string' Angular - 错误 TS2345:“NgForm”类型的参数不可分配给“Ussdthirdpartyapp”类型的参数 - Angular - error TS2345: Argument of type 'NgForm' is not assignable to parameter of type 'Ussdthirdpartyapp'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM