0.3.2 • Published 4 years ago

ionic-universal-firebase-login v0.3.2

Weekly downloads
9
License
MIT
Repository
github
Last release
4 years ago

Universal Firebase login for Ionic

Scrutinizer Code Quality Build Status code style: prettier

Do you want to implement native authentication as well as in browser authetication for your Ionic application? This package is for you!

The easiest to use flexible hybrid module for Ionic (Angular) with complete documentation and step-by-step guide. We use Firestore for authentication service and its Firestore for storing information about users.

No more pain with login implementation troubles!

Platforms:

  • Browser
  • Android
  • iOS

See documentation and guides on: https://github.com/cothema/ionic-universal-firebase-login

Video tutorial

Youtube video Ionic Firebase Login Tutorial

Play tutorial on YouTube

Supported providers

Supported login types:

  • Pop up / Native
  • Redirect (not implemented at this time)

What's included?

You can customize almost everything to your own needs (e.g. by extending, overloading or composition).

Configuration

Override or change options property of BaseAuthService

Option nameDefault valueDescription
afterSignInPage"/"Path where user will be redirected after successful sign in. If false, no redirect will be done.
providers{}List of provider configurations. See example configuration.
redirectBacktrueWhen true, user will be redirected back to requested router path after sign in
signInPage"/sign-in"Path where user will be redirected when sign in is required (and after logout). If false, no redirect will be done.
storagefalseYou can store user profile data in Firestore ('firestore') or nowhere (false).
storageUserTable"users"If you specify storage, than this table name / key will be used.
mapUserToStorageFuncYou can use custom strategy to map user model to storage. See example configuration.
storageUserFactoryFuncYou can use custom model object for user in storage. See example configuration.
userFactoryFuncYou can use custom model object for user. See example configuration.

Example configuration

import { NgModule } from "@angular/core";
import { AngularFireModule } from "@angular/fire";
import { AngularFirestoreModule } from "@angular/fire/firestore";
import { FacebookAuthModule, GoogleAuthModule, UniFirebaseLoginModule } from "ionic-universal-firebase-login";
import { environment } from "../environments/environment";
import { Player } from "./model/player.model";
import * as firebase from "firebase";

export function mapFirebaseUserToStorageFunc(user: firebase.User) {
    return {
        uid: user.uid,
        email: user.email,
        displayName: user.displayName,
        photoURL: user.photoURL,
    };
}

export function userFactoryFunc(): Player {
    return new Player();
}

@NgModule({
    imports: [
        ...
        AngularFireModule.initializeApp(environment.firebase),
        AngularFirestoreModule,
        UniFirebaseLoginModule.forRoot({
            storage: "firestore",
            storageUserTable: "players",
            signInPage: "login",
            userFactoryFunc,
            mapFirebaseUserToStorageFunc
        }),
        GoogleAuthModule,
        FacebookAuthModule,
    ],
    ...
})
export class AppModule {
}

Usage in template:

import { Component, OnInit } from "@angular/core";
import { BaseAuthService } from "ionic-universal-firebase-login";

@Component({
  selector: "app-sample",
  template: `
    Username: {{ auth.user?.displayName }}
    Email: {{ auth.user?.email }}
  `,
})
export class SamplePage {
  constructor(
    public auth: BaseAuthService,
  ) {}
}

Installation

  • Before you start, be sure that you have signed your application. It is required for native authentication on Android and iOS.

Install with npm:

npm install firebase @angular/fire ionic-universal-firebase-login
  • Go to https://firebase.google.com/create a new Firebase project (if you don't have it already).
    • Project overview → Create Web app
    • Firebase settings → General tab → Web app project (at the bottom) → Config → copy Firebase SDK snippet (Configuration is the same for all platforms, so you have to do this step only once.)
  • Edit src/environments/environment.ts and paste the config from previous step as in example below:
export const environment = {
    ...
    firebase: {
        apiKey: "XXX",
        authDomain: "XXX.firebaseapp.com",
        databaseURL: "https://XXX.firebaseio.com",
        projectId: "XXX",
        storageBucket: "XXX.appspot.com",
        messagingSenderId: "XXX",
        appId: "XXX",
        measurementId: "G-XXX",
    },
}
  • Edit src/app/app.module.ts and add:
import { NgModule } from '@angular/core'; 
import {AngularFireModule} from '@angular/fire';
import {environment} from '../environments/environment';
import {UniFirebaseLoginModule, GoogleAuthModule, FacebookAuthModule} from 'ionic-universal-firebase-login';

@NgModule({
    ...
    imports: [
        ...
        AngularFireModule.initializeApp(environment.firebase),
        UniFirebaseLoginModule.forRoot(),
        GoogleAuthModule, // if you want to use Google as a provider
        FacebookAuthModule, // if you want to use Facebook as a provider
    ]
})

App for Android platform:

See our guide for each provider:

import { NgModule } from "@angular/core";
import { PreloadAllModules, RouterModule, Routes } from "@angular/router";
import { AuthGuard } from "ionic-universal-firebase-login";

const routes: Routes = [
  {
    path: "",
    loadChildren: () =>
      import("./home.module").then(m => m.HomePageModule),
    canActivate: [AuthGuard],
    canActivateChild: [AuthGuard],
  },
  {
    path: "sign-in",
    loadChildren: () => import("./sign-in/sign-in.module").then(m => m.SignInPageModule),
  },
];

@NgModule({
  imports: [
    RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules }),
  ],
  exports: [RouterModule],
})
export class AppRoutingModule {
}

Example implementations

See example project

Real use cases:

Changelog

See CHANGELOG.md

Contribution

Feel free to send pull requests or create new issues.

License

MIT license

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.8

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.1.24

4 years ago

0.1.23

4 years ago

0.1.20

4 years ago

0.1.21

4 years ago

0.1.22

4 years ago

0.1.19

4 years ago

0.1.15

4 years ago

0.1.16

4 years ago

0.1.17

4 years ago

0.1.18

4 years ago

0.1.13

4 years ago

0.1.14

4 years ago

0.1.12

4 years ago

0.1.11

4 years ago

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.2

4 years ago

0.1.3

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago