问题描述
我真的很难理解如何获取已登录用户的当前用户 ID 并按该用户 ID 过滤列表.
I am really struggling to understand how to get the current user id of someone logged in and to filter a list by that user id.
我可以很容易地获取用户 ID,但在调用获取客户列表时它似乎不可用.
I can get the userid easily enough but it doesn't seem to be available at the time of the call to get the customer list.
如果有人能帮助我或为我指明正确的方向,我将不胜感激.
If someone could assist or point me in the right direction it would be much appreciated.
身份验证服务
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
}
});
}
}
推荐答案
AngularFire2
包已被弃用.而是使用 @angular/fire
.我在我的服务和组件中使用以下代码.
AngularFire2
package has been deprecated. Instead use @angular/fire
. I am using following code in my service and component.
authState: any = null;
constructor(
private firebaseAuth: AngularFireAuth,
) {
this.firebaseAuth.authState.subscribe( authState => {
this.authState = authState;
});
}
检查用户是否通过身份验证
check if user is authenticated
get isAuthenticated(): boolean {
return this.authState !== null;
}
检查电子邮件是否经过验证.如果您想发送电子邮件或启用禁用发送电子邮件按钮
Check if email is verified. In case you want to send email or enable disable send email button
get isEmailVerified(): boolean {
return this.isAuthenticated ? this.authState.emailVerified : false;
}
当前用户 ID
get currentUserId(): string {
return this.isAuthenticated ? this.authState.uid : null;
}
获取用户数据
get userData(): any {
if ( ! this.isAuthenticated ) {
return [];
}
return [
{
id: this.authState.uid,
displayName: this.authState.displayName,
email: this.authState.email,
phoneNumber: this.authState.phoneNumber,
photoURL: this.authState.photoURL,
}
];
}
header.component.ts 组件文件
constructor(
private auth: AuthService
) {}
header.component.html 组件文件
<a href="#" *ngIf="auth.isAuthenticated" (click)="auth.signout($event)">Sign Out</a>
这篇关于Angular2 Firebase 获取当前用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!