Angular2 Firebase获取当前用户

cco*_*ker 1 firebase firebase-authentication angularfire2 angular

我真的很难理解如何获取登录用户的当前用户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,
      });
  }
}
Run Code Online (Sandbox Code Playgroud)

客户服务

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
      }
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

Aam*_*zad 13

AngularFire2包已被弃用。而是使用@angular/fire. 我在我的服务和组件中使用以下代码。

auth.service.ts 服务文件

authState: any = null;

constructor(
  private firebaseAuth: AngularFireAuth,
) {
  this.firebaseAuth.authState.subscribe( authState => {
    this.authState = authState;
  });
} 
Run Code Online (Sandbox Code Playgroud)

检查用户是否通过身份验证

get isAuthenticated(): boolean {
    return this.authState !== null;
}
Run Code Online (Sandbox Code Playgroud)

检查电子邮件是否已验证。如果您想发送电子邮件或启用禁用发送电子邮件按钮

get isEmailVerified(): boolean {
  return this.isAuthenticated ? this.authState.emailVerified : false;
}
Run Code Online (Sandbox Code Playgroud)

当前用户 ID

get currentUserId(): string {
  return this.isAuthenticated ? this.authState.uid : null;
}
Run Code Online (Sandbox Code Playgroud)

获取用户数据

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,
    }
  ];
}
Run Code Online (Sandbox Code Playgroud)

header.component.ts 组件文件

constructor(
  private auth: AuthService
) {}
Run Code Online (Sandbox Code Playgroud)

header.component.html 组件文件

<a href="#" *ngIf="auth.isAuthenticated" (click)="auth.signout($event)">Sign Out</a>
Run Code Online (Sandbox Code Playgroud)


sug*_*rme 5

我也会添加一个firebase身份验证订阅CustomersService.通过这样做,我们确保当前用户ID可用.

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

要么

constructor(
    private af: AngularFire,
    private authService: AuthenticationService,
    private router: Router) {
      this.usersCustomerId = this.authService.userKey;
}
Run Code Online (Sandbox Code Playgroud)