【发布时间】:2017-02-06 17:04:29
【问题描述】:
我真的很难理解如何获取登录用户的当前用户 ID 并按该用户 ID 过滤列表。
我可以很容易地获得用户 ID,但在调用以获取客户列表时它似乎不可用。
如果有人可以帮助或指出正确的方向,我将不胜感激。
身份验证服务
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
}
});
}
}
【问题讨论】:
标签: angular firebase firebase-authentication angularfire2