【问题标题】:ionic, firebase: How to get ALL user emails from firebase authenticationionic,firebase:如何从 firebase 身份验证中获取所有用户电子邮件
【发布时间】:2021-04-04 16:34:34
【问题描述】:

我正在尝试获取我的 firebase 身份验证存储中所有用户的用户电子邮件,我需要此信息,以便允许用户在系统内互相发送消息。我对离子不是很有经验,所以如果这是一个愚蠢的问题,请原谅我。 我不需要登录用户的电子邮件,我已经可以访问它,但无法访问所有这些。

登录代码,不确定是否需要。

// login.page.ts
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators, FormControl } from '@angular/forms';
import { NavController } from '@ionic/angular';
import { AuthenticationService } from '../services/authentication.service';

@Component({
  selector: 'app-login',
  templateUrl: './login.page.html',
  styleUrls: ['./login.page.scss'],
})
export class LoginPage implements OnInit {

  validations_form: FormGroup;
  errorMessage: string = '';

  constructor(

    private navCtrl: NavController,
    private authService: AuthenticationService,
    private formBuilder: FormBuilder

  ) { }

  ngOnInit() {

    this.validations_form = this.formBuilder.group({
      email: new FormControl('', Validators.compose([
        Validators.required,
        Validators.pattern('^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$')
      ])),
      password: new FormControl('', Validators.compose([
        Validators.minLength(5),
        Validators.required
      ])),
    });
  }


  validation_messages = {
    'email': [
      { type: 'required', message: 'Email is required.' },
      { type: 'pattern', message: 'Please enter a valid email.' }
    ],
    'password': [
      { type: 'required', message: 'Password is required.' },
      { type: 'minlength', message: 'Password must be at least 5 characters long.' }
    ]
  };


  loginUser(value) {
    this.authService.loginUser(value)
      .then(res => {
        console.log(res);
        this.errorMessage = "";
        this.navCtrl.navigateForward('/welcome');
      }, err => {
        this.errorMessage = err.message;
      })
  }

  goToRegisterPage() {
    this.navCtrl.navigateForward('/register');
  }

}

注册码

// register.page.ts
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators, FormControl } from '@angular/forms';
import { AuthenticationService } from '../services/authentication.service';
import { NavController } from '@ionic/angular';

@Component({
  selector: 'app-register',
  templateUrl: './register.page.html',
  styleUrls: ['./register.page.scss'],
})
export class RegisterPage implements OnInit {


  validations_form: FormGroup;
  errorMessage: string = '';
  successMessage: string = '';

  validation_messages = {
    'email': [
      { type: 'required', message: 'Email is required.' },
      { type: 'pattern', message: 'Enter a valid email.' }
    ],
    'password': [
      { type: 'required', message: 'Password is required.' },
      { type: 'minlength', message: 'Password must be at least 5 characters long.' }
    ]
  };

  constructor(
    private navCtrl: NavController,
    private authService: AuthenticationService,
    private formBuilder: FormBuilder
  ) { }

  ngOnInit() {
    this.validations_form = this.formBuilder.group({
      email: new FormControl('', Validators.compose([
        Validators.required,
        Validators.pattern('^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$')
      ])),
      password: new FormControl('', Validators.compose([
        Validators.minLength(5),
        Validators.required
      ])),
    });
  }

  tryRegister(value) {
    this.authService.registerUser(value)
      .then(res => {
        console.log(res);
        this.errorMessage = "";
        this.successMessage = "Your account has been created. Please log in.";
      }, err => {
        console.log(err);
        this.errorMessage = err.message;
        this.successMessage = "";
      })
  }

  goLoginPage() {
    this.navCtrl.navigateForward('/login');
  }


}

我想要得到的会是这样的

  1. 用户点击列表/选项
  2. 用户选择一封电子邮件
  3. 键入他/她想分享的任何消息内容,我会分享一个小sn-p
<ion-select>
    <ion-select-option value="email1">email1</ion-select-option>
    <ion-select-option value="email2">email2</ion-select-option>
    <ion-select-option value="email3">email3</ion-select-option>
    <ion-select-option value="email4">email4/ion-select-option>
  </ion-select> //probably will use *ngFor to do this.

authentication service screenshot

【问题讨论】:

    标签: javascript firebase ionic-framework firebase-authentication ionic4


    【解决方案1】:

    客户端 Firebase 身份验证 SDK 无法获取系统中所有用户的电子邮件地址,因为这会带来潜在的安全风险。

    如果您的应用需要此功能,则必须自己构建。两个最常见的选项是:

    1. 实现一个使用 Firebase Admin SDK 的自定义服务器端 API,该 API 具有此类功能。
    2. 将必要的用户数据存储在数据库中,例如 Firebase 的实时数据库或 Cloud Firestore,并让客户端访问它们。

    在这两种情况下,您的应用都可以控制向您的用户公开哪些数据,从而解决安全问题。

    另见:

    【讨论】:

    • 非常感谢,花了很长时间寻找这个,我能找到的唯一信息是获取当前用户的详细信息,所以有没有一种方法可以让每当用户登录时,我都会将他的电子邮件存储在我的云火力基地还是不是这种情况?我也担心这是否可能会产生重复的结果并且可能需要大量的方法调用。
    • 您所描述的是我上面提到的第二个选项,这是我在这个用例中看到的最常见的方法。您可以防止重复结果,例如通过将用户的 UID 作为密钥来存储用户。我建议阅读我提供的一些(更深入的)链接,因为已经有很多关于这方面的信息。
    猜你喜欢
    • 2019-05-01
    • 2019-06-25
    • 2023-03-18
    • 1970-01-01
    • 2018-02-10
    • 2018-10-04
    • 2022-08-07
    • 2021-10-05
    • 2020-07-15
    相关资源
    最近更新 更多