Unverified Commit 4c3d3b71 authored by Francis Rodrigues's avatar Francis Rodrigues Committed by GitHub

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.
parent 07df897d
...@@ -33,7 +33,7 @@ const useUser = () => { ...@@ -33,7 +33,7 @@ const useUser = () => {
// Firebase updates the id token every hour, this // Firebase updates the id token every hour, this
// makes sure the react state and the cookie are // makes sure the react state and the cookie are
// both kept up to date // both kept up to date
firebase.auth().onIdTokenChanged((user) => { const cancelAuthListener = firebase.auth().onIdTokenChanged((user) => {
if (user) { if (user) {
const userData = mapUserData(user) const userData = mapUserData(user)
setUserCookie(userData) setUserCookie(userData)
...@@ -50,6 +50,10 @@ const useUser = () => { ...@@ -50,6 +50,10 @@ const useUser = () => {
return return
} }
setUser(userFromCookie) setUser(userFromCookie)
return () => {
cancelAuthListener()
}
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, []) }, [])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment