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:
parent
07df897dad
commit
4c3d3b7122
1 changed files with 5 additions and 1 deletions
|
@ -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
|
||||
}, [])
|
||||
|
||||
|
|
Loading…
Reference in a new issue