App Initialization
Here we cover the scenario in which user opens a fresh url for our website.
Two scenarios may arise :
- First time users loads our app or Returning users that had logged out of their previous session(we consider them as logged out users)
- Returning users who didn't logged out of their previous session(we consider them as logged in users)
#
Returning users#
Logged in usersHere when the logged in users(that had not logged out from the previous session) they will be auto logged in to our website. We persist the login of the users for a better user experience.
#
Persisting LoginWe use browser storage for persisting the login. Here is good news for you all, firebase has got us covered here. We don't need to do anything. It automatically stores our user
in Indexed DB
from when we can perform some action and auto-sign in the user.
When the app loads we manually dispatch an action verifyAuth()
from our configStore.js
.
configStore.js
Below is our verifyAuth()
action
From verifyAuth()
we then dispatch signInUser
action with the user
stored(in the Indexed DB)as currentUser
(we only store relevant details from user
) in the redux store and also update authenticated
to true
in the store.
Our auth
state in our redux store looks like this :
Also if you noticed we dispatched another event listenToCurrentUserProfile
and we update currentUserProfile
property in our state of the redux store.
Our profile
state in our redux store
#
Logged out UsersFor logged out users no data is present in the Indexed DB
(cleared when user logs out). Thus auto login won't pe possible here.