Integrating Firebase Authentication — Ionic Part 4

Sameeha Rahman
Jul 22 · 10 min read

Step 1 — Setting up the Firebase console

Step 2 — Linking it all up

import { AngularFireModule } from 'angularfire2';
import { AngularFireAuthModule } from 'angularfire2/auth'
import { AngularFirestoreModule } from 'angularfire2/firestore';
export const firebaseConfig = {
apiKey: "#######################################",
authDomain: "###########.firebaseapp.com",
databaseURL: "https://###########.firebaseio.com",
projectId: "###########",
storageBucket: "###########.appspot.com",
messagingSenderId: "############"
};
@NgModule({...imports: [...AngularFireModule.initializeApp(firebaseConfig), AngularFireAuthModule, AngularFirestoreModule]})

Step 3 — Writing all that code

const authObserver = afAuth.authState.subscribe(user => {
if (user) {
this.rootPage = HomePage;
authObserver.unsubscribe();
} else {
this.rootPage = LoginPage;
authObserver.unsubscribe();
}
});
<form [formGroup]="signupForm" (submit)="signupUser()" novalidate>
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
constructor(public navCtrl: NavController, public navParams: NavParams, public formBuilder: FormBuilder){}
this.signupForm = formBuilder.group({
email: ['', Validators.compose([Validators.required, EmailValidator.isValid])],...});
<ion-item class="error-message" *ngIf="!signupForm.controls.email.valid  && signupForm.controls.email.dirty">
<p>Please enter a valid email.</p>
</ion-item>
[class.invalid]="!signupForm.controls.email.valid && signupForm.controls.email.dirty"
this.loginForm = formBuilder.group({
email: ['', Validators.compose([Validators.required, EmailValidator.isValid])],
password: ['', Validators.compose([Validators.minLength(6), Validators.required])]
});
resetPwd() {
this.navCtrl.push(ResetPasswordPage);
}createAccount() {
this.navCtrl.push(RegisterPage);
}
this.resetPwdForm = formBuilder.group({
email: ['', Validators.compose([Validators.required, EmailValidator.isValid])]
});
<ion-header>
<ion-navbar>
<ion-title>To-do List</ion-title>
<ion-buttons end>
<button ion-button (click)="logout()">Logout</button>
</ion-buttons>
</ion-navbar>
</ion-header>
logout() {
return this.afAuth.auth.signOut().then(authData => {
this.app.getRootNav().setRoot(LoginPage);
});
}
@NgModule({...declarations: [...LoginPage, RegisterPage, ResetPasswordPage]...entryComponents: [...LoginPage, RegisterPage, ResetPasswordPage]})
App with Authentication
Firestore Document

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade