Cordova plugin for Firebase Authentication
Your help is appreciated. Create a PR, submit a bug or just grab me 🍺 |
---|
- Supported Platforms
- Installation
- Type Aliases
- Functions
- createUserWithEmailAndPassword
- getCurrentUser
- getIdToken
- onAuthStateChanged
- sendEmailVerification
- sendPasswordResetEmail
- signInAnonymously
- signInWithApple
- signInWithCustomToken
- signInWithEmailAndPassword
- signInWithFacebook
- signInWithGoogle
- signInWithTwitter
- signInWithVerificationId
- signOut
- updateProfile
- useAppLanguage
- useEmulator
- verifyPhoneNumber
- iOS
- Android
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.
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.
- Put
google-services.json
and/orGoogleService-Info.plist
into the root directory of your Cordova project - 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>
UserDetails: Object
Represents a user's profile information in your Firebase project's user database.
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 |
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");
Name | Type | Description |
---|---|---|
email |
string |
User account email |
password |
string |
User accound password |
Promise
<void
>
Callback when operation is completed
getCurrentUser(): Promise
<UserDetails
>
Returns the current user in the Firebase instance.
Promise
<UserDetails
>
Fulfills promise with user details
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
});
Name | Type | Description |
---|---|---|
forceRefresh |
boolean |
When true cached value is ignored |
Promise
<string
>
Fulfills promis with id token string value
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.
Name | Type | Description |
---|---|---|
callback |
(userDetails : UserDetails ) => void |
Callback function |
errorCallback? |
(error : string ) => void |
Error callback function |
fn
(): void
void
sendEmailVerification(): Promise
<void
>
Initiates email verification for the current user.
Example
cordova.plugins.firebase.auth.sendEmailVerification();
Promise
<void
>
Callback when operation is completed
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]");
Name | Type | Description |
---|---|---|
email |
string |
User account email |
Promise
<void
>
Callback when operation is completed
signInAnonymously(): Promise
<void
>
Create and use temporary anonymous account to authenticate with Firebase.
Example
cordova.plugins.firebase.auth.signInAnonymously();
Promise
<void
>
Callback when operation is completed
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);
});
Name | Type | Description |
---|---|---|
idToken |
string |
Apple's ID token string |
rawNonce |
string |
Apple's raw token string |
Promise
<void
>
Callback when operation is completed
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
- https://firebase.google.com/docs/auth/android/custom-auth
- https://firebase.google.com/docs/auth/ios/custom-auth
Name | Type | Description |
---|---|---|
authToken |
string |
Custom auth token |
Promise
<void
>
Callback when operation is completed
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");
Name | Type | Description |
---|---|---|
email |
string |
User account email |
password |
string |
User accound password |
Promise
<void
>
Callback when operation is completed
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
- https://firebase.google.com/docs/auth/android/facebook-login
- https://firebase.google.com/docs/auth/ios/facebook-login
Name | Type | Description |
---|---|---|
accessToken |
string |
Facebook's access token string |
Promise
<void
>
Callback when operation is completed
signInWithGoogle(idToken
, accessToken
): Promise
<void
>
Uses Google's idToken
and accessToken
to sign-in into firebase account.
See
- https://firebase.google.com/docs/auth/android/google-signin
- https://firebase.google.com/docs/auth/ios/google-signin
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);
});
Name | Type | Description |
---|---|---|
idToken |
string |
Google ID token |
accessToken |
string |
Google Access token |
Promise
<void
>
Callback when operation is completed
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
- https://firebase.google.com/docs/auth/android/twitter-login
- https://firebase.google.com/docs/auth/ios/twitter-login
Name | Type | Description |
---|---|---|
token |
string |
Twitter's token string |
secret |
string |
Twitter's secret string |
Promise
<void
>
Callback when operation is completed
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);
});
Name | Type | Description |
---|---|---|
verificationId |
string |
[description] |
code |
string |
6-digit SMS code |
Promise
<void
>
Callback when operation is completed
signOut(): Promise
<void
>
Signs out the current user and clears it from the disk cache.
Example
cordova.plugins.firebase.auth.signOut();
Promise
<void
>
Callback when operation is completed
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",
});
Name | Type | Description |
---|---|---|
profileDetails |
Object |
User attributes. |
profileDetails.displayName |
string |
- |
profileDetails.photoURL |
string |
- |
Promise
<void
>
Callback when operation is completed
useAppLanguage(): Promise
<void
>
Sets languageCode to the app’s current language.
Example
cordova.plugins.firebase.auth.useAppLanguage();
Promise
<void
>
Callback when operation is completed
useEmulator(host
, port
): Promise
<void
>
Sets languageCode to the app’s current language.
Example
cordova.plugins.firebase.auth.useEmulator('localhost', 8000);
Name | Type | Description |
---|---|---|
host |
string |
Emulator host name |
port |
number |
Emulator port |
Promise
<void
>
Callback when operation is completed
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.
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. |
Promise
<string
>
Fulfills promise with verificationId
to use later for signing in