@imnotout/capacitor-firebase-phoneauth

Capacitor Firebase Web & iOS phone auth wrapper


Keywords
capacitor, plugin, native, ionic, javascript, nodejs
License
GPL-3.0
Install
npm install @imnotout/capacitor-firebase-phoneauth@1.0.8

Documentation

Capacitor-firebase-phoneauth

This plugins was intended to be used internally at imnotout.com. Works only with Web and iOS. Code can be used as is or for reference under GNU GPLv3 License.

  /**
   * Requests otp from firebase to authenticate and verify phone number.
   * @param options accepts phone number as string with countrycode, eg: +919999999999
   */
  verifyPhoneNumber(options: VerifyPhoneNumberOptions): Promise<void>

  /**
   *  verifies the otp and authenticates a user
   * @param options
   */
  signinWithOtp(options: SigninWithOtpOptions): Promise<{ uid: string }>

  /**
   * Logouts the current authenticated user
   */
  signOut(): Promise<void>

  /**
   * Returns the idToken of current authenticated user
   */
  getIdToken(): Promise<{ token: String }>

  /**
   * Calls callback with user is signed in or out (err, user)
   * @param callback
   */
  onAuthStateChanged(callback: {
    (error: any, user: { uid: string } | null): void
  }): void

Special instructions for web

before calling any method set the fiebase instance to be used since we can't initialise here inside plugin, you may require firebase is required outside the plugin.

/**
 * Set firebase instance for use with web [REQUIRED].
 * You can get the initialized firebase instance from firebase.app()
 * EG: CapacitorFirebasePhoneAuth.setFirebaseInstanceForWeb(fb: firebase.app.App)
 * @param firebase
 */
setFirebaseInstanceForWeb(fb: firebase.app.App):void

Usage

import { Plugins } from "@capacitor/core"

const { CapacitorFirebasePhoneAuth } = Plugins

//Use await or a regular promise
const token = await CapacitorFirebasePhoneAuth.getIdToken()