使用Firebase进行基于角色的用户访问控制 - 语法错误(switchMap)

时间:2018-02-01 12:05:00

标签: angular typescript firebase firebase-realtime-database angularfire2

我尝试使用Firebase创建基于角色的用户访问控制,以便在用户通过身份验证时允许访问路由&& admin ...我正在尝试使用本教程:https://angularfirebase.com/lessons/role-based-permissions-and-authorization-with-firebase-auth/

我的AuthService

import { Injectable } from '@angular/core';
import {AngularFireAuth} from 'angularfire2/auth';
import {Observable} from 'rxjs/Observable';
import {User} from '../models/user.model';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
import {AngularFireDatabase} from 'angularfire2/database';
import * as firebase from 'firebase/app';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/switchMap';

@Injectable()
export class AuthService {
  user: BehaviorSubject<User> = new BehaviorSubject(null)

  constructor(private afAuth: AngularFireAuth, private db: AngularFireDatabase) {
    this.afAuth.authState
      .switchMap(auth => {
        if (auth) {
          /// signed in
          return this.db.object('users/' + auth.uid)
        } else {
          /// not signed in
          return Observable.of(null)
        }
      })
      .subscribe(user => {
        this.user.next(user)
      })
  }

}

我的用户模型

export interface Roles {
  reader:boolean;
  admin?:boolean;
}

export class User {
  id:number;
  admin:boolean;
  activated:boolean;
  usertype:string;
  profile : {
    title:string;
    firstname:string;
    lastname:string;
    gender:string;
    houseNr:string;
    plz:string;
    street:string;
    place:string;
    nation:string;
    email:string;
    companyname:string;
    ustID:string;
    phone:number;
    mobile:number;
    fax:number;
    intPhonePrefix:string;
    intMobilePrefix:string;
    intFaxPrefix:string;
    phonePrefix:string;
    mobilePrefix:string;
    faxPrefix:string;
    bankDepositor:string;
    bankIban:string;
    bankBic:string;
    bankInstitute:string;
  }
  roles: Roles;

  constructor() {
    this.roles = { reader: true }
  }
}

但我在.switchMap(auth => {上收到语法错误:

enter image description here

我不知道错误是什么..

1 个答案:

答案 0 :(得分:0)

您必须使用管道。 \subsection