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#
InstallationWe first install firebase SDK
#
Create Configuration FileWe 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 FirebaseFirebase makes authentication easy by providing methods for various platforms. We just need to call the methods for using the authentication feature.
#
Sign up new userscreateUserWithEmailAndPassword() 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.js
file 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 usersThe 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.js
file.
#
Social Sign inHere 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 userSimply call signOut method to sign out the logged in user.
#
Useful ResourcesTo 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.