How to Setup Firebase
Firebase account and project registration
Follow these steps to register your Firebase account and create a Firebase project:
-
Register a Firebase account at Firebase console . You can use your Google account to authenticate at Firebase.
-
Click Create project
!> Note: If you have a Google project registered for your mobile app, select it from the Project name dropdown menu.
You can also edit your Project ID if you need. A unique ID is assigned automatically to each project. This ID is used in publicly visible Firebase features. For example, it will be used in database URLs and in your Firebase Hosting subdomain. If you need to use a specific subdomain, you can change it.
-
Fill in the fields required (Project name, Project ID, etc.) and click Continue.
-
Configure Google Analytics for your project and click Create project.
Then click Continue.
-
Select platform for which you need Firebase
If you already have an app in your project, go to project Overview page and click Add another app link:
Connect Firebase SDK
Here is a step by step guide how to connect Firebase SDK to your Web Project:
-
Copy the code from Add Firebase to your web app screen and paste it in the tag of your HTML page before other script tags:
!> Note: The above code snippet should be used to initialize Firebase Javascript SDK for Authentication, Cloud Storage, Realtime Database, and Cloud Firestore. You can include only the features that you need. In our case Authentication (firebase-auth) component is necessary. If you use FCM push notifications on Web, then you need to include Firebase Cloud Messaging (firebase-messaging) component as well.
-
From the CDN you need to include the following components:
In case you are using a bundler like Browserify or webpack, you can just require() the components that you use:
Firebase authentication
This option allows users in your app authenticate themselves via phone number. If you use this method for user authentication, the user receives an SMS with verification code and authenticates via that code in your app.
You need to follow these steps to add Firebase authentication to your Web project:
-
Go to Firebase console >> Authentication >> Sign-in method section:
-
Enable Phone number sign-in method:
-
In Firebase console >> Authentication >> Sign-in method section check Authorized domains block and if your domain that will host your app is not added there, add it:
Then enter your domain name on the pop-up window that appears and click Add:
-
Set up reCAPTCHA verification:
!> Note: If you use Firebase SDK’s
RecaptchaVerifier
object, Firebase creates and handles any necessary client keys and secrets automatically, so you do not need to set up a reCAPTCHA client manually.reCAPTCHA options:
- Invisible reCAPTCHA
RecaptchaVerifier
object supports invisible reCAPTCHA, which can verify the user without requiring any user action.In order to set it you need to create a
RecaptchaVerifier
object with thesize
parameter set toinvisible
, specifying the ID of the button that submits your sign-in form:
Optional features:
- Localization
reCAPTCHA can be localized by updating the language code on the Auth instance before rendering the reCAPTCHA. This localization will apply to the SMS message with verification code sent to the user as well.
- reCAPTCHA parameters
It is possible to set callback functions on the RecaptchaVerifier
object that are called when the user solves the reCAPTCHA or the reCAPTCHA expires before the user submits the form:
- Pre-render the reCAPTCHA
It is possible to pre-render the reCAPTCHA before you submit a sign-in request, if you call render
:
After render
resolves, you get the reCAPTCHA’s widget ID. You can use it to make calls to the reCAPTCHA API:
-
Send a verification code to user’s phone:
In order to request that Firebase send an authentication code to the user’s phone by SMS, set an interface that prompts users to provide their phone number, and then call
signInWithPhoneNumber
as follows:- Get the user’s phone number
!> Important note: As a best practice please do not forget to inform your users that if they use phone sign-in, they might receive an SMS message for verification and standard rates apply.
- Call
signInWithPhoneNumber
, passing to it the user’s phone number andRecaptchaVerifier
you created earlier:
If
signInWithPhoneNumber
results in an error, reset reCAPTCHA so that the user can try verifying again:!> Note:
signInWithPhoneNumber
method issues reCAPTCHA challenge to the user, and if the user passes the challenge, requests that Firebase Authentication send an SMS message containing a verification code to the user’s phone. -
Sign in the user with the verification code:
-
Once the call to
signInWithPhoneNumber
succeeds, you need to prompt the user to type the verification code they received by SMS. -
Then, sign in the user by passing the code to
confirm
method ofConfirmationResult
object that was passed tosignInWithPhoneNumber
’s fulfillment handler (that is, itsthen
block):
If the call to
confirm
succeeded, the user is successfully signed in.- Get the intermediate
AuthCredential
object:
If you need to get an
AuthCredential
object for the user’s account, pass the verification code from the confirmation result and the verification code toPhoneAuthProvider.credential
instead of callingconfirm
:Then, you can sign in the user with the credential:
-
-
Get Firebase accessToken after SMS code verification as follows:
-
Get your Project ID from your Firebase console:
-
Pass your Firebase
project_id
and Firebaseaccess_token
parameters tologin
method: -
Try running a test.
Once your user is logged in successfully, you will find him/her in your Dashboard >> Your App >> Users section.
So now you know how to use Firebase features in your ConnectyCube apps. If you have any difficulties - please let us know via support channel