2017-02-06 12 views
0

ログインしているユーザーの現在のユーザーIDを取得し、そのユーザーIDでリストをフィルタリングする方法を理解するのは本当に苦労しています。Angular2 Firebase現在のユーザーを取得

私はuseridを簡単に取得できますが、顧客リストを取得するための呼び出し時には利用できないようです。

誰かが正しい方向に私を助けたり指し示すことができたら、それは非常に感謝しています。

認証サービス

import { Injectable } from "@angular/core"; 
import { AngularFire, AuthProviders, AuthMethods, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2'; 
import { UserModel } from '../users/models/user.model'; 

@Injectable() 
export class AuthenticationService { 

    public displayName: string; 
    public userKey: string; 
    public user: UserModel; 

    constructor(public af: AngularFire) { 
    this.af.auth.subscribe(
     (auth) => { 
     if (auth != null) { 
      this.user = this.af.database.object('users/' + auth.uid); 
      this.userKey = auth.uid; 
     } 
     }); 
    } 

    logout() { 
    return this.af.auth.logout(); 
    } 

    loginWithEmail(email, password) { 
    return this.af.auth.login({ 
     email: email, 
     password: password, 
    }, 
     { 
     provider: AuthProviders.Password, 
     method: AuthMethods.Password, 
     }); 
    } 
} 

カスタマーサービス

import { Injectable } from '@angular/core'; 
import { Router } from '@angular/router'; 
import { AngularFire, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2'; 

import { AuthenticationService } from '../authentication/authentication.service'; 

import { CustomerModel } from './models/customer.model'; 

@Injectable() 
export class CustomersService { 

    customersRef: string = '/customers/'; 
    customer: any; 
    usersCustomerId: string; 

    constructor(
    private af: AngularFire, 
    private authService: AuthenticationService, 
    private router: Router) { } 

    getAllCustomers(): FirebaseListObservable<CustomerModel[]> { 

    this.usersCustomerId = this.authService.userKey; 
    console.log(this.usersCustomerId); 

    return this.af.database.list(this.customersRef, { 
     query: { 
     orderByChild: 'uid', 
     equalTo: this.usersCustomerId 
     } 
    }); 
    } 
} 

答えて

3

私もCustomersServiceにfirebase認証へのサブスクリプションを追加します。これにより、現在のユーザーIDが使用可能であることを確認しています。

constructor(
    private af: AngularFire, 
    private router: Router) { 
     this.af.auth.subscribe(auth => { 
     if(auth) { 
      this.usersCustomerId = auth.uid;  
     } 
    }) 
} 

または

constructor(
    private af: AngularFire, 
    private authService: AuthenticationService, 
    private router: Router) { 
     this.usersCustomerId = this.authService.userKey; 
} 
0

uがuが必要なデータを取得するために.subscribe()方法を使用することができ、ここでexempleです:

this.authserve.auth.subscribe(user=>{ 
    if(user) 
    this.curruser = user.email; 
}) 

PS:

  1. authserveAngularFireAuth instence
  2. authauthserve
  3. curruser内に作成Observableあるだけのシンプルなstring変数
です
関連する問題