We are going to use the new VueJS Composition API to separate out Firebase integration into a VueJS Application. We will focus in this blog post on determining if there is already user information associated with the application saved or if the user needs to login to the application. If the user needs to login to the application we will provide the ability to login and logout of firebase using the composition api application structure
We are assuming there is a basic understanding of VueJS and Firebase. Addition information on setting up you application to use the Firebase Javascript SDK can be found at this link
The properties are derived from the use of vuejs new composition api that has been announced for 3.0 but is accessible in 2.0 by using a vue-composition-api plugin
This function will do the firebase authentication check and set the user property or the error property when checking the authentication status of the user. While firebase is making the api call the loading property will also be set.
All we are doing here is making the firebase api call and updating the properties in the function based on the result of the api call. Since these state variables are reactive, as they are updated it will cause the screen/UI be be re-rendered
import{toRefs,reactive}from"@vue/composition-api";importfirebasefrom"firebase";// Required for side-effectsimport"firebase/firestore";// initialize firebase, this is directly from the firebase documentation// regarding getting started for the webif (firebase.apps.length===0){constfirebaseConfig={/* YOUR CONFIGURATION GOES HERE */};firebase.initializeApp(firebaseConfig);}exportdefaultfunction(){// our reactive properties...letstate=reactive({user:null,loading:true,error:null});// make the firebase call to listen for change in auth state,// we have set initial loading status to true so nothing happens on UI // side until loading is set to falsefirebase.auth().onAuthStateChanged(_user=>{if (_user){state.user=_user;}else{state.user=null;}state.loading=false;});// return all of the properties from the functionreturn{...toRefs(state)};}
Vue Composition API Function: useLogin
This function will do the firebase login and logout functionality; this will set the user property or the error property when checking the authentication status of the user. While firebase is making the api call the loading property will also be set.
Important to note that when we make login call and set error if it happens, we don't need to set the user object because the other composition function is listening for change in user state and when detected it will update the user object
import{toRefs,reactive,computed}from"@vue/composition-api";importfirebasefrom"firebase";// Required for side-effectsimport"firebase/firestore";exportdefaultfunction(){letstate=reactive({error:null,username:null,password:null,user:null});/**
* have this value `isValid` get updated when the dependent properties
* are changed in the composition function
*/letisValid=computed(()=>{let{username,password}=state;return (username!==null&&username.length!==0&&password!==null&&password.length!==0);});constlogin=()=>{firebase.auth().signInWithEmailAndPassword(state.username,state.password).then(()=>{},error=>(state.error=error)).catch(error=>{// Handle Errors here.state.error=error;});};constlogout=()=>{firebase.auth().signOut().then(()=>{},error=>(state.error=error)).catch(error=>{// Handle Errors here.state.error=error;});};return{// return all of the properties from the function...toRefs(state),// return all of the computed value to make sure we// get non null values for username/password from the // functionisValid,// pass back a login and logout function to be utilized // by the login formlogin,logout};}
Returning to App Component
Now that we have all of the composition function set, we can walk through what is really happening in the application.
<script>importThingListfrom"./components/ThingList.vue";importLoginFormfrom"./components/LoginForm.vue";// composition api importsimport{computed}from"@vue/composition-api";// our custom composition functions for firebase auth check// and for logging in and logging out of firebaseimportuseAuthfrom"./use-auth";importuseLoginfrom"./use-login";exportdefault{name:"app",components:{ThingList,LoginForm},setup(){// load in the authentication propertieslet{user,loading,error}=useAuth();// load in the login functionletloginState=useLogin();return{user,loading,// here we need to ensure that we get the error from // either the loginState or the error returned from the useAuth// functionerror:computed(()=>(loginState.error||error).value),// set the logout function from the usLogin composition functionlogout:loginState.logout};}};</script><style>#app{font-family:"Avenir",Helvetica,Arial,sans-serif;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;color:#2c3e50;padding:60px;}</style>
When we call this function
let{user,loading,error}=useAuth();
we will call firebase to see if there is already user information available from a previous login, if so the user object is set accordingly otherwise we have no user, no error and the application will render the view because the values are reactive and we will be then showing the Login Form
(a) when firebase is checking for user we are showing loading
(b1) when loading is complete do we have an error?
(b2) when loading is complete do we have a user? If so then render the ThingList Component and some user information
(b3) when loading is complete do not we have a user? If so render the Login Form
Back to the Login Form to see how this all fits together; when the application loads this component we have access to the useLogin composition function. This function returns as it state object
username
password
error
and returns two functions
login
logout
and returns a computed function we use to check if fields are valid
isValid
After the login method is called, we will get an error which will be displayed on the template, or we will get a successful outcome which will change the state of the useAuth composition function. It will then return a valid user object which would then cause the application to render the ThingList component and hide the LoginForm component.
<template><div><inputtype="text"placeholder="username"v-model="username"/><inputtype="password"placeholder="password"v-model="password"/><button@click="login":disabled="!isValid">LOGIN</button><p>{{error}}</p></div></template><script>// import { reactive, toRefs } from "@vue/composition-api";importuseLoginfrom"../use-login";exportdefault{setup(){letstate=useLogin();return{...state}}};</script>
Conclusion
The Vue Composition API is a pretty interesting addition to VueJS 3.0 release and I think it provides similar functionality to react-hooks so vuejs developers should not feel like they are missing out on anything here.
if you find some errors/typos/mistakes or something isn't clear, please leave a comment below.
use the new VueJS Composition API to seperate out Firebase integration into a VueJS Application.
PART ONE: VueJS with Composition API and Ionic Framework Components
Overview
We are going to use the new VueJS Composition API to seperate out Firebase integration into a VueJS Application. We will focus in this blog post on determining if there is already user information associated with the application saved or if the user needs to login to the application. If the user needs to login to the application we will provide the ability to login and logout of firebase using the composition api application structure
Clearly Innovative is a minority-owned solutions provider that develops digital products. We shape ideas into viable products and transform client needs into enhanced technology solutions. As a leader in early adoption and implementation of cutting edge technologies, Clearly Innovative provides services focused on product strategy, user experience, design and development. According to CEO, Aaron Saunders "We are not just designers and developers, but end-to-end digital solution providers." Clearly Innovative has created a tech education program, Clearly Innovative Education, whose mission is to create a world where people from underrepresented backgrounds can have a seat at the digital table as creators, innovators and entrepreneurs.
#TheFutureIsWrittenInCode
The Future is Written in Code series, as part of Inclusive Innovation Incubator, provides introductory and advanced programming classes as well as coding courses with a focus on business and entrepreneurship. Select programming offered includes Coding, UI/UX, Coding & Business, Coding & Entrepreneurship, Business Canvassing, Entrepreneurship: Developing Your Idea into App, to name a few. Please contact info@in3dc.com to find out more!