0.3.13 • Published 2 years ago

@tambroseavid/capacitor-firebase-authentication v0.3.13

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Maintainers

MaintainerGitHubSocial
Robin Genzrobingenz@robin_genz

Installation

npm install @robingenz/capacitor-firebase-authentication firebase
npx cap sync

Add Firebase to your project if you haven't already (Android / iOS / Web).

On iOS, verify that this function is included in your app's AppDelegate.swift:

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
  return ApplicationDelegateProxy.shared.application(app, open: url, options: options)
}

The further installation steps depend on the selected authentication method:

Attention: Please note that this plugin uses third-party SDKs to offer native sign-in. These SDKs can initialize on their own and collect various data. Here you can find more information.

Configuration

These configuration values are available:

PropTypeDescriptionDefault
skipNativeAuthbooleanConfigure whether the plugin should skip the native authentication. Only needed if you want to use the Firebase JavaScript SDK. Only available for Android and iOS.false
providersstring[]Configure which providers you want to use so that only the providers you need are fully initialized. If you do not configure any providers, they will be all initialized. Please note that this does not prevent the automatic initialization of third-party SDKs. Only available for Android and iOS."apple.com", "facebook.com", "github.com", "google.com", "microsoft.com", "playgames.google.com", "twitter.com", "yahoo.com", "phone", "password"

Examples

In capacitor.config.json:

{
  "plugins": {
    "FirebaseAuthentication": {
      "skipNativeAuth": false,
      "providers": ["apple.com", "google.com"]
    }
  }
}

In capacitor.config.ts:

/// <reference types="@capacitor/firebase-authentication" />

import { CapacitorConfig } from '@capacitor/cli';

const config: CapacitorConfig = {
  plugins: {
    FirebaseAuthentication: {
      skipNativeAuth: false,
      providers: ["apple.com", "google.com"],
    },
  },
};

export default config;

Usage

import { FirebaseAuthentication } from '@robingenz/capacitor-firebase-authentication';

const getCurrentUser = async () => {
  const result = await FirebaseAuthentication.getCurrentUser();
  return result.user;
};

const getIdToken = async () => {
  const result = await FirebaseAuthentication.getIdToken();
  return result.token;
};

const setLanguageCode = async () => {
  await FirebaseAuthentication.setLanguageCode({ languageCode: 'en-US' });
};

const signInWithApple = async () => {
  await FirebaseAuthentication.signInWithApple();
};

const signInWithFacebook = async () => {
  await FirebaseAuthentication.signInWithFacebook();
};

const signInWithGithub = async () => {
  await FirebaseAuthentication.signInWithGithub();
};

const signInWithGoogle = async () => {
  await FirebaseAuthentication.signInWithGoogle();
};

const signInWithMicrosoft = async () => {
  await FirebaseAuthentication.signInWithMicrosoft();
};

const signInWithPlayGames = async () => {
  await FirebaseAuthentication.signInWithPlayGames();
};

const signInWithPhoneNumber = async () => {
  const { verificationId } = await FirebaseAuthentication.signInWithPhoneNumber(
    {
      phoneNumber: '123456789',
    },
  );
  const verificationCode = window.prompt(
    'Please enter the verification code that was sent to your mobile device.',
  );
  await FirebaseAuthentication.signInWithPhoneNumber({
    verificationId,
    verificationCode,
  });
};

const signInWithTwitter = async () => {
  await FirebaseAuthentication.signInWithTwitter();
};

const signInWithYahoo = async () => {
  await FirebaseAuthentication.signInWithYahoo();
};

const signOut = async () => {
  await FirebaseAuthentication.signOut();
};

const useAppLanguage = async () => {
  await FirebaseAuthentication.useAppLanguage();
};

API

getCurrentUser()

getCurrentUser() => Promise<GetCurrentUserResult>

Fetches the currently signed-in user.

Returns: Promise<GetCurrentUserResult>


getIdToken(...)

getIdToken(options?: GetIdTokenOptions | undefined) => Promise<GetIdTokenResult>

Fetches the Firebase Auth ID Token for the currently signed-in user.

ParamType
optionsGetIdTokenOptions

Returns: Promise<GetIdTokenResult>


sendPasswordResetEmail(...)

sendPasswordResetEmail(options?: SendPasswordResetEmailOptions | undefined) => Promise<SendPasswordResetEmailResult>

Sends a password reset email.

ParamType
optionsSendPasswordResetEmailOptions

Returns: Promise<SendPasswordResetEmailResult>


setLanguageCode(...)

setLanguageCode(options: SetLanguageCodeOptions) => Promise<void>

Sets the user-facing language code for auth operations.

ParamType
optionsSetLanguageCodeOptions

signInWithApple(...)

signInWithApple(options?: SignInOptions | undefined) => Promise<SignInResult>

Starts the Apple sign-in flow.

ParamType
optionsSignInOptions

Returns: Promise<SignInResult>


signInWithFacebook(...)

signInWithFacebook(options?: SignInOptions | undefined) => Promise<SignInResult>

Starts the Facebook sign-in flow.

ParamType
optionsSignInOptions

Returns: Promise<SignInResult>


signInWithGithub(...)

signInWithGithub(options?: SignInOptions | undefined) => Promise<SignInResult>

Starts the GitHub sign-in flow.

ParamType
optionsSignInOptions

Returns: Promise<SignInResult>


signInWithGoogle(...)

signInWithGoogle(options?: SignInOptions | undefined) => Promise<SignInResult>

Starts the Google sign-in flow.

ParamType
optionsSignInOptions

Returns: Promise<SignInResult>


signInWithMicrosoft(...)

signInWithMicrosoft(options?: SignInOptions | undefined) => Promise<SignInResult>

Starts the Microsoft sign-in flow.

ParamType
optionsSignInOptions

Returns: Promise<SignInResult>


signInWithPlayGames(...)

signInWithPlayGames(options?: SignInOptions | undefined) => Promise<SignInResult>

Starts the Play Games sign-in flow.

ParamType
optionsSignInOptions

Returns: Promise<SignInResult>


signInWithTwitter(...)

signInWithTwitter(options?: SignInOptions | undefined) => Promise<SignInResult>

Starts the Twitter sign-in flow.

ParamType
optionsSignInOptions

Returns: Promise<SignInResult>


signInWithYahoo(...)

signInWithYahoo(options?: SignInOptions | undefined) => Promise<SignInResult>

Starts the Yahoo sign-in flow.

ParamType
optionsSignInOptions

Returns: Promise<SignInResult>


signInWithPassword(...)

signInWithPassword(options?: SignInWithPasswordOptions | undefined) => Promise<SignInResult>

Starts the password sign-in flow.

ParamType
optionsSignInWithPasswordOptions

Returns: Promise<SignInResult>


signInWithPhoneNumber(...)

signInWithPhoneNumber(options: SignInWithPhoneNumberOptions) => Promise<SignInWithPhoneNumberResult>

Starts the sign-in flow using a phone number.

Either the phone number or the verification code and verification ID must be provided.

Only available for Android and iOS.

ParamType
optionsSignInWithPhoneNumberOptions

Returns: Promise<SignInWithPhoneNumberResult>


signInWithCustomToken(...)

signInWithCustomToken(options: SignInWithCustomTokenOptions) => Promise<SignInResult>

Starts the Custom Token sign-in flow.

This method cannot be used in combination with skipNativeAuth on Android and iOS. In this case you have to use the signInWithCustomToken interface of the Firebase JS SDK directly.

ParamType
optionsSignInWithCustomTokenOptions

Returns: Promise<SignInResult>


signOut()

signOut() => Promise<void>

Starts the sign-out flow.


useAppLanguage()

useAppLanguage() => Promise<void>

Sets the user-facing language code to be the default app language.


addListener('authStateChange', ...)

addListener(eventName: 'authStateChange', listenerFunc: AuthStateChangeListener) => Promise<PluginListenerHandle> & PluginListenerHandle

Listen for the user's sign-in state changes.

ParamType
eventName'authStateChange'
listenerFuncAuthStateChangeListener

Returns: Promise<PluginListenerHandle> & PluginListenerHandle


removeAllListeners()

removeAllListeners() => Promise<void>

Remove all listeners for this plugin.


Interfaces

GetCurrentUserResult

PropTypeDescription
userUser | nullThe currently signed-in user, or null if there isn't any.

User

PropType
displayNamestring | null
emailstring | null
emailVerifiedboolean
isAnonymousboolean
phoneNumberstring | null
photoUrlstring | null
providerIdstring
tenantIdstring | null
uidstring

GetIdTokenResult

PropTypeDescription
tokenstringThe Firebase Auth ID token JWT string.

GetIdTokenOptions

PropTypeDescription
forceRefreshbooleanForce refresh regardless of token expiration.

SendPasswordResetEmailResult

PropTypeDescription
successbooleanConfirm the request status.

SendPasswordResetEmailOptions

PropTypeDescription
emailstringThe email address to send the reset message.

SetLanguageCodeOptions

PropTypeDescription
languageCodestringBCP 47 language code. Example: en-US.

SignInResult

PropTypeDescription
userUser | nullThe currently signed-in user, or null if there isn't any.
credentialAuthCredential | nullCredentials returned by an auth provider.

AuthCredential

PropTypeDescription
providerIdstringThe authentication provider ID for the credential. Example: google.com.
accessTokenstringThe OAuth access token associated with the credential if it belongs to an OAuth provider.
idTokenstringThe OAuth ID token associated with the credential if it belongs to an OIDC provider.
secretstringThe OAuth access token secret associated with the credential if it belongs to an OAuth 1.0 provider.
noncestringThe random string used to make sure that the ID token you get was granted specifically in response to your app's authentication request.

SignInOptions

PropTypeDescription
customParametersSignInCustomParameter[]Configures custom parameters to be passed to the identity provider during the OAuth sign-in flow.

SignInCustomParameter

PropTypeDescription
keystringThe custom parameter key (e.g. login_hint).
valuestringThe custom parameter value (e.g. user@firstadd.onmicrosoft.com).

SignInWithPasswordOptions

PropTypeDescription
emailstringThe email address to be verified.
passwordstringThe password to be verified.

SignInWithPhoneNumberResult

PropTypeDescription
verificationIdstringThe verification ID, which is needed to identify the verification code.

SignInWithPhoneNumberOptions

PropTypeDescription
phoneNumberstringThe phone number to be verified.
verificationIdstringThe verification ID which will be returned when signInWithPhoneNumber is called for the first time. The verificationCode must also be provided.
verificationCodestringThe verification code from the SMS message. The verificationId must also be provided.

SignInWithCustomTokenOptions

PropTypeDescription
tokenstringThe custom token to sign in with.

PluginListenerHandle

PropType
remove() => Promise<void>

AuthStateChange

PropTypeDescription
userUser | nullThe currently signed-in user, or null if there isn't any.

Type Aliases

AuthStateChangeListener

Callback to receive the user's sign-in state change notifications.

(change: AuthStateChange): void

FAQ

  1. What does this plugin do?
    This plugin enables the use of Firebase Authentication in a Capacitor app. It uses the Firebase SDK for Java (Android), Swift (iOS) and JavaScript.
  2. What is the difference between the web implementation of this plugin and the Firebase JS SDK?
    The web implementation of this plugin encapsulates the Firebase JS SDK and enables a consistent interface across all platforms. You can decide if you prefer to use the web implementation or the Firebase JS SDK.
  3. How can I use this plugin with the Firebase JavaScript SDK?
    See here.

Changelog

See CHANGELOG.md.

License

See LICENSE.