Skip to content

Latest commit

 

History

History
626 lines (400 loc) · 15.6 KB

File metadata and controls

626 lines (400 loc) · 15.6 KB

Cordova plugin for Firebase Authentication

NPM version NPM downloads NPM total downloads Twitter

Donate Your help is appreciated. Create a PR, submit a bug or just grab me 🍺

Index

Supported Platforms

  • iOS
  • Android

Installation

cordova plugin add cordova-plugin-firebase-authentication

Use variables IOS_FIREBASE_POD_VERSION and ANDROID_FIREBASE_BOM_VERSION to override dependency versions for Firebase SDKs:

$ cordova plugin add cordova-plugin-firebase-authentication \
--variable IOS_FIREBASE_POD_VERSION="9.3.0" \
--variable ANDROID_FIREBASE_BOM_VERSION="30.3.1"

To use phone number authentication on iOS, your app must be able to receive silent APNs notifications from Firebase. For iOS 8.0 and above silent notifications do not require explicit user consent and is therefore unaffected by a user declining to receive APNs notifications in the app. Thus, the app does not need to request user permission to receive push notifications when implementing Firebase phone number auth.

Adding required configuration files

Cordova supports resource-file tag for easy copying resources files. Firebase SDK requires google-services.json on Android and GoogleService-Info.plist on iOS platforms.

  1. Put google-services.json and/or GoogleService-Info.plist into the root directory of your Cordova project
  2. Add new tag for Android platform
<platform name="android">
    ...
    <resource-file src="google-services.json" target="app/google-services.json" />
</platform>
...
<platform name="ios">
    ...
    <resource-file src="GoogleService-Info.plist" />
</platform>

Type Aliases

UserDetails

UserDetails: Object

Represents a user's profile information in your Firebase project's user database.

Type declaration

Name Type Description
displayName string Main display name of this user from the Firebase project's user database
email string Main email address of the user, as stored in the Firebase project's user database.
emailVerified boolean true if the user's email is verified.
phoneNumber string | null Phone number of the user, as stored in the Firebase project's user database, or null if none exists.
photoURL string URL of this user's main profile picture, as stored in the Firebase project's user database.
providerId string -
uid string String used to uniquely identify your user in your Firebase project's user database

Functions

createUserWithEmailAndPassword

createUserWithEmailAndPassword(email, password): Promise<void>

Creates a new user account with the given email address and password.

Example

cordova.plugins.firebase.auth.createUserWithEmailAndPassword("[email protected]", "pa55w0rd");

Parameters

Name Type Description
email string User account email
password string User accound password

Returns

Promise<void>

Callback when operation is completed


getCurrentUser

getCurrentUser(): Promise<UserDetails>

Returns the current user in the Firebase instance.

Returns

Promise<UserDetails>

Fulfills promise with user details


getIdToken

getIdToken(forceRefresh): Promise<string>

Returns a JWT token used to identify the user to a Firebase service.

Example

cordova.plugins.firebase.auth.getIdToken().then(function(idToken) {
    // send token to server
});

Parameters

Name Type Description
forceRefresh boolean When true cached value is ignored

Returns

Promise<string>

Fulfills promis with id token string value


onAuthStateChanged

onAuthStateChanged(callback, errorCallback?): () => void

Registers a block as an auth state did change listener. To be invoked when:

  • The block is registered as a listener,
  • A user with a different UID from the current user has signed in, or
  • The current user has signed out.

Parameters

Name Type Description
callback (userDetails: UserDetails) => void Callback function
errorCallback? (error: string) => void Error callback function

Returns

fn

(): void

Returns

void


sendEmailVerification

sendEmailVerification(): Promise<void>

Initiates email verification for the current user.

Example

cordova.plugins.firebase.auth.sendEmailVerification();

Returns

Promise<void>

Callback when operation is completed


sendPasswordResetEmail

sendPasswordResetEmail(email): Promise<void>

Triggers the Firebase Authentication backend to send a password-reset email to the given email address, which must correspond to an existing user of your app.

Example

cordova.plugins.firebase.auth.sendPasswordResetEmail("[email protected]");

Parameters

Name Type Description
email string User account email

Returns

Promise<void>

Callback when operation is completed


signInAnonymously

signInAnonymously(): Promise<void>

Create and use temporary anonymous account to authenticate with Firebase.

Example

cordova.plugins.firebase.auth.signInAnonymously();

Returns

Promise<void>

Callback when operation is completed


signInWithApple

signInWithApple(idToken, rawNonce): Promise<void>

Uses Apples's idToken and rawNonce to sign-in into firebase account. For getting idToken (rawNonce is optional) you can use cordova-plugin-sign-in-with-apple (or any other cordova plugin for Apple Sign-In).

See

Example

// below we use cordova-plugin-sign-in-with-apple to trigger Apple Login UI
cordova.plugins.SignInWithApple.signin({
    requestedScopes: [0, 1]
}, function(res) {
    cordova.plugins.firebase.auth.signInWithApple(res.identityToken).then(function() {
        console.log("Firebase logged in with Apple");
    }, function(err) {
        console.error("Firebase login failed", err);
    });
}, function(err) {
    console.error("Apple signin failed", err);
});

Parameters

Name Type Description
idToken string Apple's ID token string
rawNonce string Apple's raw token string

Returns

Promise<void>

Callback when operation is completed


signInWithCustomToken

signInWithCustomToken(authToken): Promise<void>

You can integrate Firebase Authentication with a custom authentication system by modifying your authentication server to produce custom signed tokens when a user successfully signs in. Your app receives this token and uses it to authenticate with Firebase.

See

Parameters

Name Type Description
authToken string Custom auth token

Returns

Promise<void>

Callback when operation is completed


signInWithEmailAndPassword

signInWithEmailAndPassword(email, password): Promise<void>

Triggers the Firebase Authentication backend to send a password-reset email to the given email address, which must correspond to an existing user of your app.

Example

cordova.plugins.firebase.auth.signInWithEmailAndPassword("[email protected]", "pa55w0rd");

Parameters

Name Type Description
email string User account email
password string User accound password

Returns

Promise<void>

Callback when operation is completed


signInWithFacebook

signInWithFacebook(accessToken): Promise<void>

Uses Facebook's accessToken to sign-in into firebase account. In order to retrieve those tokens follow instructions for iOS and Android from Firebase docs.

See

Parameters

Name Type Description
accessToken string Facebook's access token string

Returns

Promise<void>

Callback when operation is completed


signInWithGoogle

signInWithGoogle(idToken, accessToken): Promise<void>

Uses Google's idToken and accessToken to sign-in into firebase account.

See

Example

// Below we use cordova-plugin-googleplus to trigger Google Login UI
window.plugins.googleplus.login({
    scopes: '... ',
    webClientId: '1234...',
    offline: true
}, function(res) {
    cordova.plugins.firebase.auth.signInWithGoogle(res.idToken, res.accessToken).then(function() {
        console.log("Firebase logged in with Google");
    }, function(err) {
        console.error("Firebase login failed", err);
    });
}, function(err) {
    console.error("Google login failed", err);
});

Parameters

Name Type Description
idToken string Google ID token
accessToken string Google Access token

Returns

Promise<void>

Callback when operation is completed


signInWithTwitter

signInWithTwitter(token, secret): Promise<void>

Uses Twitter's token and secret to sign-in into firebase account. In order to retrieve those tokens follow instructions for iOS and Android from Firebase docs.

See

Parameters

Name Type Description
token string Twitter's token string
secret string Twitter's secret string

Returns

Promise<void>

Callback when operation is completed


signInWithVerificationId

signInWithVerificationId(verificationId, code): Promise<void>

Completes phone number verification process and use it to sign in.

Example

cordova.plugins.firebase.auth.verifyPhoneNumber("+123456789").then(function(verificationId) {
    var code = prompt("Enter verification code");
    if (code) {
        return cordova.plugins.firebase.auth.signInWithVerificationId(verificationId, code);
    }
}).catch(function(err) {
    console.error("Phone number verification failed", err);
});

Parameters

Name Type Description
verificationId string [description]
code string 6-digit SMS code

Returns

Promise<void>

Callback when operation is completed


signOut

signOut(): Promise<void>

Signs out the current user and clears it from the disk cache.

Example

cordova.plugins.firebase.auth.signOut();

Returns

Promise<void>

Callback when operation is completed


updateProfile

updateProfile(profileDetails): Promise<void>

Updates the current user's profile data. Passing a null value will delete the current attribute's value, but not passing a property won't change the current attribute's value.

Example

cordova.plugins.firebase.auth.updateProfile({
    displayName: "Jane Q. User",
    photoURL: "https://example.com/jane-q-user/profile.jpg",
});

Parameters

Name Type Description
profileDetails Object User attributes.
profileDetails.displayName string -
profileDetails.photoURL string -

Returns

Promise<void>

Callback when operation is completed


useAppLanguage

useAppLanguage(): Promise<void>

Sets languageCode to the app’s current language.

Example

cordova.plugins.firebase.auth.useAppLanguage();

Returns

Promise<void>

Callback when operation is completed


useEmulator

useEmulator(host, port): Promise<void>

Sets languageCode to the app’s current language.

Example

cordova.plugins.firebase.auth.useEmulator('localhost', 8000);

Parameters

Name Type Description
host string Emulator host name
port number Emulator port

Returns

Promise<void>

Callback when operation is completed


verifyPhoneNumber

verifyPhoneNumber(phoneNumber, timeoutMillis?): Promise<string>

Starts the phone number verification process for the given phone number.

Android supports auto-verify and instant device verification. You must register onAuthStateChanged to get callback on instant verification.

Maximum allowed value for timeout is 2 minutes. Use 0 to disable SMS-auto-retrieval. If you specify a positive value less than 30 seconds, library will default to 30 seconds.

Parameters

Name Type Description
phoneNumber string Phone number in international format
timeoutMillis? number Maximum amount of time you are willing to wait for SMS auto-retrieval to be completed by the library.

Returns

Promise<string>

Fulfills promise with verificationId to use later for signing in