Reputation: 35
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
<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
Upvotes: 1
Views: 1667
Reputation: 13139
I have found a workaround, you can open the auth page where you can see the user emails.
https://console.firebase.google.com/project/your-project/authentication/users
Then just open inspect element and open the console, use this code
// Select all email elements from the first column of the table
const emails = Array.from(document.querySelectorAll('table tbody tr td:first-child'))
.map(cell => cell.innerText.trim())
.filter(email => email.includes('@')); // Ensure it's an email
// Log the emails separated by a newline (for one below the other in Excel)
console.log(emails.join('\n'));
// (Optional) Copy emails to clipboard
copy(emails.join('\n'));
This will list the emails and also copy them to clipboard, after that just go to Google Spreadsheets and paste those emails.
Be sure to put at least 250 entries to be shown at the time.
I know this is not the way maybe you should do it, but for me, I had a huge user base and I forgot at first to store the user emails into my database, this saved me before implementing it.
Upvotes: 1
Reputation: 599856
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:
In both cases your app controls what data is exposed about your users, which addresses the security concern.
Also see:
Upvotes: 1