Giter VIP home page Giter VIP logo

cordova-plugin-firebase-authentication's Introduction

Cordova plugin for Firebase Authentication

NPM version NPM downloads Twitter

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

Index

Installation

cordova plugin add cordova-plugin-firebase-authentication

Use variables ANDROID_FIREBASE_AUTH_VERSION or IOS_FIREBASE_AUTH_VERSION to override dependency versions for Firebase SDKs.

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.

Supported Platforms

  • iOS
  • Android

User authorization

Unlike v1 of the plugin in v2 you must register onAuthStateChanged callback to be notified when signIn* or signOut methods are completed.

onAuthStateChanged(callback)

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.
cordova.plugins.firebase.auth.onAuthStateChanged(function(userInfo) {
    if (userInfo) {
        // user was signed in
    } else {
        // user was signed out
    }
});

createUserWithEmailAndPassword(email, password)

Tries to create a new user account with the given email address and password.

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

sendEmailVerification()

Initiates email verification for the current user.

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

sendPasswordResetEmail(email)

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.

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

signInWithEmailAndPassword(email, password)

Asynchronously signs in using an email and password.

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

verifyPhoneNumber(phoneNumber, timeout)

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

NOTE: Android supports auto-verify and instant device verification. Therefore in that cases it doesn't make sense to ask for sms code. It's recommended to register onAuthStateChanged callback to be notified on auto sign-in.

timeout [milliseconds] is the maximum amount of time you are willing to wait for SMS auto-retrieval to be completed by the library. Maximum allowed value 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.

cordova.plugins.firebase.auth.verifyPhoneNumber("+123456789", 30000).then(function(verificationId) {
    // pass verificationId to signInWithVerificationId
});

signInWithVerificationId(verificationId, smsCode)

Asynchronously signs in using verificationId and 6-digit SMS code.

cordova.plugins.firebase.auth.signInWithVerificationId("djgfioerjg34", "123456");

signInAnonymously()

Create and use temporary anonymous account to authenticate with Firebase.

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

signInWithGoogle(idToken, accessToken)

Uses Google's idToken and accessToken to sign-in into firebase account. In order to retriave those tokens you can use cordova-plugin-googleplus (or any other cordova plugin for Google Sign-In).

cordova plugin add cordova-plugin-firebase-authentication
cordova plugin add cordova-plugin-googleplus --variable REVERSED_CLIENT_ID=myreversedclientid --variable WEB_APPLICATION_CLIENT_ID=mywebapplicationclientid

Now trigger signin dialog UI to popup:

window.plugins.googleplus.login({
    'scopes': '... ', // optional, space-separated list of scopes, If not included or empty, defaults to `profile` and `email`.
    'webClientId': 'client id of the web app/server side', // optional clientId of your Web application from Credentials settings of your project - On Android, this MUST be included to get an idToken. On iOS, it is not required.
    'offline': true // optional, but requires the webClientId - if set to true the plugin will also return a serverAuthCode, which can be used to grant offline access to a non-Google server
}, function(res) {
    // signin into Firebase
    cordova.plugins.firebase.auth.signInWithGoogle(res.idToken, res.accessToken).then(function() {
        console.log("Firebase logged in with Google");
    });
}, function(err) {
    console.error("login failed", err);
});

signInWithApple(idToken, rawNonce)

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).

cordova plugin add cordova-plugin-firebase-authentication
cordova plugin add cordova-plugin-sign-in-with-apple

Then use code snippet below to signin into Firebase:

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("signin failed", err);
});

signInWithFacebook(accessToken)

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

signInWithTwitter(token, secret)

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

signInWithCustomToken(idToken)

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 Android and iOS for more info.

signOut()

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

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

Get/set user state

Every method call returns a promise which is optionally fulfilled with an appropriate value.

getCurrentUser()

Returns the current user in the Firebase instance.

cordova.plugins.firebase.auth.getCurrentUser().then(function(userInfo) {
    // user information or null if not logged in
})

getIdToken(forceRefresh)

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

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

setLanguageCode(languageCode)

Set's the current user language code. The string used to set this property must be a language code that follows BCP 47.

useAppLanguage()

Sets languageCode to the app’s current language.

cordova-plugin-firebase-authentication's People

Contributors

andreszs avatar cairin avatar chemerisuk avatar douglaszaltron avatar javierpaytef avatar mariuszgola avatar solomonrodion avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.