AngularFire2:访问用户配置文件

时间:2016-12-10 14:37:27

标签: angular angularfire2

我绕圈子,希望有人能指出我正确的方向。

业务问题 我有一个AngularFire2应用程序,我想执行以下步骤:

  1. 用户在Firebase中注册为用户
  2. 然后,用户填写一份注册表,其中包含其他信息(他们所服务的客户或组织)。然后他们点击保存
  3. 创建新客户
  4. 使用在步骤3中创建的客户的客户密钥创建用户配置文件
    1. 用户被重定向到客户列表页面,它只显示登录用户个人资料中具有客户ID的所有客户
  5. 我已经完成了所有步骤,但我似乎无法解决的是如何在触发客户服务上的获取所有客户的呼叫之前获取用户配置文件信息。

    理想情况下,我想要做的是将用户服务注入客户服务部门,以确定当前用户是谁以及他们的客户ID是什么,因此我可以将其作为参数传递给获取所有客户功能。

    我可以轻松调用AuthService并在firebase authstate上订阅uid,但我无法知道如何从那里查找用户配置文件。

    代码

    AuthService

        import { Injectable } from '@angular/core';
    import { AngularFire, AuthProviders, AuthMethods, FirebaseAuthState, FirebaseListObservable } from 'angularfire2';
    import { Observable } from 'rxjs/Rx';
    import { Router } from '@angular/router';
    
    import { UserModel } from '../../user/models/user.model';
    
    @Injectable()
    export class AuthService {
    
      user: UserModel;
      userProfile: any;
      userId: string;
      usersRef: string = '/users/';
    
      constructor(private af: AngularFire, private router: Router) {
    
        this.af.auth.subscribe(authState => {
          this.user = authState;
          console.log('authService Constructor: ', this.user)
        });
      }
    
      register(email?: string, password?: string) {
        this.af.auth.createUser({ email, password })
          .then(response => this.router.navigate(['/user/add/']))
          .catch(error => console.log(error));
      }
    
      isAuth(): UserModel {
        return this.user;
      }
    }
    

    用户服务

    import { Injectable } from '@angular/core';
    import { Router } from '@angular/router';
    import { AngularFire, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2';
    
    import { AuthService } from '../../auth/services/auth.service';
    import { FirebaseService } from '../../core/services/firebase.service';
    import { CustomerService } from '../../customer/services/customer.service';
    
    import { UserModel } from '../models/user.model';
    import { CustomerModel } from '../../customer/models/customer.model';
    
    @Injectable()
    export class UserService {
      usersRef: string = '/users/';
      user: UserModel;
      customer: CustomerModel = new CustomerModel(null, null, null, null, null, null, null, null);
      customerKey: string;
      userId: string;
    
      constructor(
        private af: AngularFire,
        private authService: AuthService,
        private customerService: CustomerService,
        private firebaseService: FirebaseService,
        private router: Router) {
    
    
      }
    
      getAllUsers(): FirebaseListObservable<UserModel[]> {
        return this.af.database.list(this.usersRef);
      }
    
      getUser(){
        return this.af.database.list(this.usersRef, {
          query: {
            orderByKey: true,
            equalTo: this.userId
          }
        })
      }
    
      addUser(user: any) {
    
        this.createNewCustomer(user.organisation);
    
        this.createUser(user);
    
        this.saveUser();
    
      }
    
      createUser(user: UserModel) {
        this.user = user;
        this.user.createdDate = user.createdDate = Date.now();
        this.user.customerId = this.customerKey;
      }
    
      saveUser() {
        this.af.database.list(this.usersRef).push(this.user)
          .then(response => {
            this.redirectToCustomerList();
          });
      }
    
      createNewCustomer(organisation: string) {
        this.customerKey = this.firebaseService.createUniqueKey('customers');
        this.customer.organisation = organisation;
        this.customerService.addCustomer(this.customer, this.customerKey);
      }
    
      redirectToCustomerList() {
    
        this.router.navigate(['/customer/list']);
      }
    
    }
    

    客户服务

    import { Injectable } from '@angular/core';
    
    import { AngularFire, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2';
    
    import { CustomerModel } from '../models/customer.model';
    
    @Injectable()
    export class CustomerService {
    
      customersRef: string = '/customers/';
      customer: CustomerModel;
      userId: string;
    
      constructor(private af: AngularFire) {
    
      }
    
      getAllCustomers(): FirebaseListObservable<CustomerModel[]> {
    
        return this.af.database.list(this.customersRef, {
          query: {
            orderByKey: true,
            equalTo: this.userId
          }
        })
      }
    
      getCustomer(id: string): FirebaseObjectObservable<any> {
        return this.af.database.object(this.customersRef + id);
      }
    
      addCustomer(customer: CustomerModel, customerKey?: string): any {
        customer.createdDate = Date.now();
        if (customerKey) {
          this.af.database.object(this.customersRef + customerKey).set(customer);
        } else {
          this.af.database.list(this.customersRef).push(customer);
        }
        return customerKey;
      }
    
      updateCustomer(customerIndex: string, customer: CustomerModel) {
        this.af.database.object(this.customersRef).update(customer);
      }
    
    
    }
    

1 个答案:

答案 0 :(得分:0)

这样的东西?

customerForCurrentUser$ = this.af.auth
  .map(user => user.$key)
  .switchMap(key => this.customerService.getCustomer(key))