Firebase
We'll be using Firebase to manage our entire backend.
Firebase provides a ready-to-go backend. It provides many useful services but the services we're interested in are Firestore, Storage, Hosting.
note
To keep our project clean and organized we'll create two separated filefirebaseService.js and firestoreService.js each containing their respective functions
Adding Firebase to our Project#
Installation#
We first install firebase SDK
Create Configuration File#
We include only specific Firebase products to our project to reduce our bundle size and also for efficiency. We'll name this file firebase.js
Authentication with Firebase#
Firebase makes authentication easy by providing methods for various platforms. We just need to call the methods for using the authentication feature.
Sign up new users#
createUserWithEmailAndPassword() returns user credentials. By using updateProfile() which is yet another method that firebase provides we are updating the user's displayName as shown above.
setUserProfileData() is a function defined infirestoreService.jsfile which basically is creating a document in firestore with user account info(here we store only the fields necessary) . Don't worry we'll look on how firestore works shortly.
Sign in existing users#
The above function returns a promise so you need to await it in the try catch block from where you're calling this function.
The above function will go in
firebaseService.jsfile.
Social Sign in#
Here we first create an instance of the Google or Facebook provider object. Then, to be able to sign in with a pop-up window we call signInWithPopup
Sign out user#
Simply call signOut method to sign out the logged in user.
Useful Resources#
To read more about firebase Auth visit : firebase.auth
User Credential is returned when signing in/up:
User Credential Contains : User + Auth Credentials + Additional User Info
important
All the firebase functions mentioned above returns a promise. Don't forget to handle them accordingly.
