Firebase Authentication in Angular with AngularFire2


Firebase provides a very simple way to setup authentication in your apps. Here we’ll explore how to setup a simple email/password signup and authentication workflow for Angular 2+ apps using the AngularFire2library.

The first step will be to create a new Firebase project and enable the Email/Password sign-in method under the Authentication section of the Firebase console.

Installing and Setting Up AngularFire2

Let’s get started by installing the necessary packages using npm. This will add the Firebase SDK, AngularFire2 and a promise-polyfill dependency to your project:

$ npm install firebase angularfire2 --save
$ npm install promise-polyfill --save-exact

Now let’s add our Firebase API and project credentials to the environment variable of our project. You’ll find these values in your Firebase console if you click on Add Firebase to your web app:


export const environment = {
  production: false,
  firebase: {
    apiKey: 'XXXXXXXXXXX',
    authDomain: 'XXXXXXXXXXX',
    databaseURL: 'XXXXXXXXXXX',
    projectId: 'XXXXXXXXXXX',
    storageBucket: 'XXXXXXXXXXX',
    messagingSenderId: 'XXXXXXXXXXX'

Then we’ll configure our app module with AngularFireModule and AngularFireAuthModule. Notice also that we’re importing and providing an AuthService. We’ll create that service next:


// ...

import { AngularFireModule } from 'angularfire2';
import { environment } from '../environments/environment';
import { AngularFireAuthModule } from 'angularfire2/auth';

import { AuthService } from './auth.service';

  declarations: [
  imports: [
    // ...
  providers: [AuthService],
  bootstrap: [AppComponent]
export class AppModule { }

Creating the Auth Service

Our service will be a central place that allows us to loginsignup or logoutusers, so we’ll define methods for these 3 actions. All the authentication logic will be in the service, which will allow us to create components that don’t need to implement any auth logic and will help keep our components simple.

You can create the skeleton for the service using the Angular CLI using this command:

$ ng g s auth

And here’s the implementation of the service, making use of AngularFireAuth:


import { Injectable } from '@angular/core';

import { AngularFireAuth } from 'angularfire2/auth';
import * as firebase from 'firebase/app';

import { Observable } from 'rxjs/Observable';

export class AuthService {
  user: Observable<firebase.User>;

  constructor(private firebaseAuth: AngularFireAuth) {
    this.user = firebaseAuth.authState;

  signup(email: string, password: string) {
      .createUserWithEmailAndPassword(email, password)
      .then(value => {
        console.log('Success!', value);
      .catch(err => {
        console.log('Something went wrong:',err.message);

  login(email: string, password: string) {
      .signInWithEmailAndPassword(email, password)
      .then(value => {
        console.log('Nice, it worked!');
      .catch(err => {
        console.log('Something went wrong:',err.message);

  logout() {


You’ll notice that the methods available on AngularFireAuth.auth all return promises so we can use then and catch to deal with the success and error states respectively.

We use createUserWithEmailAndPassword and signInWithEmailAndPassword here since we’re setting up email/password auth, but equivalent methods are available for authentication with Twitter, Facebook or Google.

Component Class and Template

Now that our auth service is in place, it’s really simple to create a component that allows for logging in, signing up or logging out:


import { Component } from '@angular/core';
import { AuthService } from './auth.service';

@Component({ ... })
export class AppComponent {
  email: string;
  password: string;

  constructor(public authService: AuthService) {}

  signup() {
    this.authService.signup(, this.password); = this.password = '';

  login() {
    this.authService.login(, this.password); = this.password = '';    

  logout() {

We inject the service in the component’s constructor and then define local methods that call the equivalent methods on the auth service. We inject the service with the public keyword instead of private so that we can access the service properties directly from the template too.

The template is very simple and uses the async pipe on the authService’s userobject to determine if there’s a logged-in user or not:


<h1 *ngIf="authService.user | async">Welcome {{ (authService.user | async)?.email }}!</h1>

<div *ngIf="!(authService.user | async)">
  <input type="text" [(ngModel)]="email" placeholder="email">
  <input type="password" [(ngModel)]="password" placeholder="email">

  <button (click)="signup()" [disabled]="!email || !password">

  <button (click)="login()" [disabled]="!email || !password">

<button (click)="logout()" *ngIf="authService.user | async">

Our input fields two-way bind to the email and password values in our component class using ngModel and the banana in a box syntax.

Best Resources to learn more about Angular, Firebase and ionic 4

Leave a Reply

Your email address will not be published. Required fields are marked *