I’m working on an forum app that uses Firebase and sqlite. The signup page, login page and reset password via sending an email are all fully functional.
Currently, when resetting the password a toast notification alerts the user that a password reset requested. I’ve been trying to add a function that checks if the email exist and alerts the user if the email does not exist.
Here is what I have so fare
reset-password.ts
import { IonicPage, NavController, NavParams, ViewController, LoadingController } from 'ionic-angular';
import { Firebase } from '@ionic-native/firebase';
import { Component } from '@angular/core';
import { FormGroup, FormBuilder, Validators, AbstractControl } from '@angular/forms';
import { UserCredentials } from '../../shared/interfaces';
import { DataService } from '../../shared/services/data.service';
import { AuthService } from '../../shared/services/auth.service';
import { EmailValidator } from '../../shared/validators/email.validator';
import { ToastController } from 'ionic-angular';
@IonicPage()
@Component({
selector: 'page-reset-password',
templateUrl: 'reset-password.html',
})
export class ResetPasswordPage {
resetPasswordForm: FormGroup;
email: AbstractControl;
myParam: string;
constructor(
public navCtrl: NavController,
public viewCtrl: ViewController,
params: NavParams,
public loadingCtrl: LoadingController,
private fb: FormBuilder,
private authService: AuthService,
private toastCtrl: ToastController,) {
this.myParam = params.get('myParam');
this.resetPasswordForm = this.fb.group({
'email': ['', Validators.compose([Validators.required, Validators.pattern(/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/)])]
});
this.email = this.resetPasswordForm.controls['email'];
}
ionViewDidLoad() {
console.log('ionViewDidLoad ResetPasswordPage');
}
// dismiss window
dismiss() {
this.viewCtrl.dismiss();
}
// Reset password then dismiss window upon submition
passReset: boolean = false;
resetPassword() {
if (this.resetPasswordForm.valid){
var self = this;
this.authService.resetPassword(this.resetPasswordForm.value['email'])
.then(() => {
this.passReset = true
let toast = self.toastCtrl.create({
message: 'ارسال تعليمات استعادة كلمة المرور...',
duration: 4000,
position: 'top'
})
toast.present();
});
this.viewCtrl.dismiss();
}
}
}
auth.service.ts
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Firebase } from '@ionic-native/firebase';
import { UserCredentials } from '../interfaces';
import { FirebaseApp, ZoneScheduler } from 'angularfire2';
import { AngularFireDatabase } from "angularfire2/database";
declare var firebase: any;
@Injectable()
export class AuthService {
usersRef: any = firebase.database().ref('users');
constructor(
) {
}
// password reset
resetPassword(email: string) {
var auth = firebase.auth();
return auth.sendPasswordResetEmail(email)
.then(() => console.log("email sent"))
.catch((error) => console.log(error))
}
registerUser(user: UserCredentials) {
return firebase.auth().createUserWithEmailAndPassword(user.email, user.password);
}
signInUser(email: string, password: string) {
return firebase.auth().signInWithEmailAndPassword(email, password);
}
signOut() {
return firebase.auth().signOut();
}
addUser(username: string, dateOfBirth: string, uid: string) {
this.usersRef.child(uid).update({
username: username,
dateOfBirth: dateOfBirth
});
}
getLoggedInUser() {
var user = firebase.auth().currentUser;
return user;
}
onAuthStateChanged(callback) {
return firebase.auth().onAuthStateChanged(callback);
}
// not working properly
checkEmailExist() {
var register = firebase.database().ref("Users/{{userId}}");
register.once('value', function(snapshot) {
if(snapshot.val().hasOwnProperty('email')) {
console.log("Email exist.");
}
else {
console.log("Email not exist.");
/* Code to push email to the specific user */
}
});
}
}
Thank you and really appreciate the help!