ReallySmallSoftware / cordova-plugin-firebaseui-auth

Cordova plugin to provide FirebaseUI Authentication
Apache License 2.0
18 stars 1 forks source link

cordova-plugin-firebaseui-auth

A FirebaseUI Auth plugin to enable easy authentication using a number of different providers.

What is FirebaseUI?

From the documentation (https://opensource.google.com/projects/firebaseui):

A UI library for Firebase, including binding for the realtime database, authentication and storage.

Supported platforms

This plugin supports the following platforms:

Support providers

The following providers are currently supported:

Follow the instructions for each of the platforms and providers you wish you use in order to add firebase to your app:

Installation

cordova plugin add cordova-plugin-firebaseui-auth --variable ANDROID_FIREBASE_CORE_VERSION=16.0.0
    --variable ANDROID_FIREBASE_AUTH_VERSION=16.0.1
    --variable ANDROID_PLAY_SERVICES_AUTH_VERSION=15.0.1
    --variable ANDROID_FIREBASEUI_VERSION=3.3.1
    --variable ANDROID_FACEBOOK_SDK_VERSION=4.31.0
    --variable FACEBOOK_APPLICATION_ID=12345678
    --variable FACEBOOK_DISPLAY_NAME="My application"
    --variable REVERSED_CLIENT_ID="com.googleusercontent.apps.9999999999-xxxxxxxxxxxxxxxxxxxxxxxxxx"
    --variable COLOR_PRIMARY="#ffffff"
    --variable COLOR_DARK_PRIMARY="#555555"
    --variable COLOR_LIGHT_PRIMARY="#aaaaaa"
    --variable COLOR_ACCENT="#7C4DFF"
    --variable COLOR_SECONDARY="#FFC107"
    --variable COLOR_CONTROL="#ffffff"
    --variable COLOR_BACKGROUND="#000000"

or

phonegap plugin add cordova-plugin-firebaseui-auth  --variable ANDROID_FIREBASE_CORE_VERSION=16.0.0
    --variable ANDROID_PLAY_SERVICES_AUTH_VERSION=16.0.1
    --variable ANDROID_FIREBASE_AUTH_VERSION=15.1.0
    --variable ANDROID_FIREBASEUI_VERSION=3.3.1
    --variable ANDROID_FACEBOOK_SDK_VERSION=4.31.0
    --variable FACEBOOK_APPLICATION_ID=12345678
    --variable FACEBOOK_DISPLAY_NAME="My application"
    --variable REVERSED_CLIENT_ID="com.googleusercontent.apps.9999999999-xxxxxxxxxxxxxxxxxxxxxxxxxx"
    --variable COLOR_PRIMARY="#ffffff"
    --variable COLOR_DARK_PRIMARY="#555555"
    --variable COLOR_LIGHT_PRIMARY="#aaaaaa"
    --variable COLOR_ACCENT="#7C4DFF"
    --variable COLOR_SECONDARY="#FFC107"
    --variable COLOR_CONTROL="#ffffff"
    --variable COLOR_BACKGROUND="#000000"

Any variables that are not supplied will use default values. For credential based variables (FACEBOOK_APPLICATION_ID, REVERSED_CLIENT_ID and REVERSED_CLIENT_ID) this will result in the provider not working until you supply real values.

Not all variables are relevant to all providers:

Firebase configuration

Android

You must ensure that google-services.json is put in the correct location. This can be achieved using the following in your config.xml:

<platform name="android">
    <resource-file src="https://github.com/ReallySmallSoftware/cordova-plugin-firebaseui-auth/raw/master/google-services.json" target="google-services.json" />
</platform>

MainActivity

In order for FirebaseUI to work on Android the default Cordova MainActivity needs to be replaced with a class that inherits from a FragmentActivity.

This plugin therefore depends on the cordova-plugin-android-fragmentactivity plugin to enable this.

iOS

iOS requires GoogleService-Info.plist is put in the correct location. Similarly this can be done as follws:

<platform name="ios">
    <resource-file src="https://github.com/ReallySmallSoftware/cordova-plugin-firebaseui-auth/raw/master/GoogleService-Info.plist" />
</platform>

Keychain Sharing Capability

If using multiple Firebase plugins it may be necessary to enable this.

Dependencies

Promises

This plugin uses Promises. If you want to use this with Android 4.4 then you will need to include a Promise polyfill.

Getting started

This guide will assume familiarity with Firebase and FirebaseUI.

Create a new FirebaseAuthUI instance:

   FirebaseUIAuth.initialise({
      "providers": ["GOOGLE", "FACEBOOK", "EMAIL"],
      "tosUrl" : "http://www.myapp.co.uk/terms.html",
      "privacyPolicyUrl" : "http://www.myapp.co.uk/privacy.html",
      "theme" : "themeName",
      "logo" : "logoName",
      "uiElement" : "mywebelement",
      "anonymous" : true|false,
      "smartLockEnabled" : true|false,
      "smartLockHints" : true|false,
      "iOSDisable" : true | false
    }).then(function(firebaseUIAuth) {
      myfirebaseUIAuthInstance = firebaseUIAuth;
    });

This is initialised as a Promise to allow the Browser implementation to dynamically add a reference to the FirestoreUI Javascript SDK.

Not all of the above options will function on all platforms:

Browser configuration

In order for the above initialisation to work on a browser you need to include some extra configuration in the form of the browser section show below:

FirebaseUIAuth.initialise({
   "providers": ["GOOGLE", "FACEBOOK", "EMAIL", "ANONYMOUS"],
   "tosUrl" : "http://www.myapp.co.uk/terms.html",
   "privacyPolicyUrl" : "http://www.myapp.co.uk/privacy.html",
   "theme" : "themeName",
   "logo" : "logoName",
   "uiElement" : "mywebelement",
   "anonymous" : true|false,
   "smartLockEnabled" : true|false,
   "smartLockHints" : true|false,
   "browser" : {
     apiKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
     authDomain: 'xxxxxxxxxxxxxx.firebaseapp.com',
     projectId: 'xxxxxxxxxxxxx',
   }
 }).then(function(firebaseUIAuth) {
   myfirebaseUIAuthInstance = firebaseUIAuth;
 });

Methods

signInAnonymously()

Call this to sign in anonymously.

signIn()

Call this to start the sign in process based on the above configuration. This can raise the following events:

signinsuccess

The user has signed in successfully. The following data is returned:

{
  name: 'user display name',
  email: 'user email',
  emailVerified: true | false,
  photoUrl: 'url of user image if available',
  id: <user id>,
  newUser: true | false
}

signinfailure

Sign in failed for some reason. The following is returned:

{
  code: <failure code>,
  message: 'failure message'
}

signinaborted

Sign in has been aborted by the user, by closing the sign in window.

signOut()

Sign the current user out of the application. This can raise the following events:

signoutsuccess

The user has signed out successfully.

signoutfailure

Sign out failed for some reason.

getToken()

Get an access token, returning a Promise.

getCurrentUser()

Get the currently signed in user.

deleteUser()

Delete the current user. This can raise the following events:

deleteusersuccess

The user was deleted successfully.

deleteuserfailure

The user was not deleted.

sendEmailVerification()

Sends a verification email for the current user. This can raise the following events:

emailverificationsent

The email verification was sent.

emailverificationnotsent

The email verification was not sent.

reloadUser

Reloads user details. Useful after a verification link has been used. A signinsuccess event will be raised.

What platform configuration is carried out?

For iOS and Android a number of platform files are added or updated based on the supplied configuration.

Android

res/values/strings.xml This has the following added:

res/values/color.xml This is either created or updated with the colour definitions supplied.

res/values/styles.xml This is either created or updated with a style that uses the above colour definitions.

iOS

*-Info.plist

The following keys are added:

<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleTypeRole</key>
    <string>Editor</string>
    <key>CFBundleURLName</key>
    <string>REVERSED_CLIENT_ID</string>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>$REVERSED_CLIENT_ID</string>
    </array>
    <key>CFBundleTypeRole</key>
    <string>Editor</string>
    <key>CFBundleURLName</key>
    <string></string>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>fb$FACEBOOK_APPLICATION_ID</string>
    </array>
  </dict>
</array>
<key>FacebookAppID</key>
<string>$FACEBOOK_APPLICATION_ID</string>
<key>FacebookDisplayName</key>
<string>$FACEBOOK_DISPLAY_NAME</string>
<key>LSApplicationQueriesSchemes</key>
<array>
  <string>fbapi</string>
  <string>fb-messenger-api</string>
  <string>fbauth2</string>
  <string>fbshareextensions</string>
</array>

Browser security

In order to ensure the browser implementation works, it will be necessary to configure the Content-Security-Policy meta tag with something similar to the following:

<meta http-equiv="Content-Security-Policy" content="default-src 'self' gap://ready file://* *;
                  style-src 'self' 'unsafe-inline'
                        https://*.gstatic.com
                        https://*.googleapis.com
                        https://*.firebase.com
                        https://*.firebaseio.com;
                  script-src 'self' 'unsafe-inline' 'unsafe-eval'
                        https://*.gstatic.com
                        https://*.googleapis.com
                        https://*.google.com
                        https://*.firebase.com
                        https://*.firebaseio.com">

Typescript

Support is now included for typescript. Use the following to reference the typescript definitions:

/// <reference types="cordova-plugin-firebaseui-auth" />

FirebaseUIAuth.initialise(options)
    .then((firebaseUIAuth: FirebaseUIAuth.FirebaseUIAuth) => {
        firebaseUIAuth.signIn();
    });

You may also need to add an external to webpack.config.ls:

  externals: {
    'cordova/exec': "cordova/exec"
  },

History

3.0.0

2.0.0

1.5.0

1.4.0

1.3.0

1.2.0

1.1.0

1.0.2

0.0.10

0.0.9

0.0.8

0.0.7

0.0.6

0.0.5

0.0.4

0.0.3

0.0.2

0.0.1