gladly-team / next-firebase-auth

Simple Firebase authentication for all Next.js rendering strategies
https://nfa-example-git-v1x-gladly-team.vercel.app/
MIT License
1.34k stars 291 forks source link

Await auth request when calling signOut #557

Open kmjennison opened 1 year ago

kmjennison commented 1 year ago

The signOut method signs out the user on on the Firebase JS SDK but it does not await the "logout" API request. This can cause unexpected behavior.

From discussion: https://github.com/gladly-team/next-firebase-auth/discussions/556


On our index page I have a top menu which hosts the Sign Out button. The onClick of Sign Out looks like this.

const router = useRouter()
const signOut = async () => {
    await authUser.signOut()
    router.reload()
  }

I expected this to change my index page context, on the router.reload of the page. But at this time I still the authUser.id being present even after reload. The page only works correctly when I click a manual refresh on browser.

export const getServerSideProps = withAuthUserTokenSSR()(async context => {
  let isAuthenticated = false
  const authUser = context.AuthUser
  if (authUser.id) {
    console.log(authUser.id)
    isAuthenticated = true
  }
return {
    props: {
      isAuthenticated,
    },
  }
}

How can I reflect the correct logout state to my SSR page for updating the UI for a guest user?

Originally posted by @ahetawal-p in https://github.com/gladly-team/next-firebase-auth/discussions/556