1

i'm trying to get the user email of ALL the users that are in my firebase authentication store, i need this information so i can allow user to message one another within the system. im not very experienced with ionic so pardon me if it's a stupid question. i do not need the logged in users email, i already have access to it but am having trouble accessing all of them.

login code, not sure if exactly needed.

// 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 code

// 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');
  }


}

what i'm trying to get would be something like

  1. user clicks a list/options
  2. user picks one email
  3. types any message content he/she wants to share, i'll share a small snippet
<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

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Haris
  • 35
  • 6

1 Answers1

0

There is no way in the client-side Firebase Authentication SDK to get the email addresses of all users in the system, as that would be a potential security risk.

If your app needs this functionality, you'll have to build it yourself. The two most common options are:

  1. Implement a custom server-side API that uses the Firebase Admin SDK, which has such functionality.
  2. Store the necessary user data in a database, such as Firebase's Realtime Database or Cloud Firestore, and have the client access it there.

In both cases your app controls what data is exposed about your users, which addresses the security concern.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • thanks a lot, spent a long time looking for this and the only information i could find is getting the current users details, so is there a way that whenever a user logs in i store his email in my cloud firebase or would that not be the case too? i'm also concerned if that's possible if it would produce duplicate results and would require a lot of method calling perhaps. – Haris Dec 27 '20 at 17:44
  • What you're describing is the second option I mention above, which is the most common approach I see for this use-case. You can prevent duplicate results, for example by storing the users with their UID as their key. I recommend reading some of the (deeper) links I provided, as there's a lot of information on this already. – Frank van Puffelen Dec 27 '20 at 18:16