Fix: with-firebase-authentication event listener (#16057)

Reference to discussion https://github.com/vercel/next.js/discussions/16010
Bug fixes to `with-firebase-authentication` example that enter in a loop `maximum update depth exceeded errors` becase of the `onIdTokenChanged` Firebase auth method.
I added a clean up event listener.
This commit is contained in:
Francis Rodrigues 2020-08-10 22:05:42 -03:00 committed by GitHub
parent 07df897dad
commit 4c3d3b7122
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -33,7 +33,7 @@ const useUser = () => {
// Firebase updates the id token every hour, this
// makes sure the react state and the cookie are
// both kept up to date
firebase.auth().onIdTokenChanged((user) => {
const cancelAuthListener = firebase.auth().onIdTokenChanged((user) => {
if (user) {
const userData = mapUserData(user)
setUserCookie(userData)
@ -50,6 +50,10 @@ const useUser = () => {
return
}
setUser(userFromCookie)
return () => {
cancelAuthListener()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])