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

withAuthUserTokenSSR: AuthUser email is null #563

Closed ahetawal-p closed 1 year ago

ahetawal-p commented 1 year ago

Describe the bug If I return to a logged in state page after some time, the AuthUser from context doest not contain any email data in it. While the client side shows the logged in state correctly. Once I log out and log back in, everything starts working. We decide on making certain api calls only if the user is logged in on server side, but in this case it breaks, as client seems to say user is logged but thats not the case on server ?

export const getServerSideProps = withAuthUserTokenSSR()(async context => {
  const tasksArray = []
  const authUser = context.AuthUser

  const featuredBoardTask = fetchFeaturedBoardByToken().catch(_ => null)
  tasksArray.push(featuredBoardTask)

 // THIS CHECK IS NULL
  if (authUser.email) {
  const token = await AuthUser.getIdToken()
    const myProfileTask = fetchMyProfile(token).catch(_ => null)
    tasksArray.push(myProfileTask)
  }
  const [featuredBoard = null, userDetails = null] = await Promise.all(tasksArray)

  return {
    props: {
      featured: featuredBoard,
      userDetails,
    },
  }
})

export default withAuthUser<HomePageProps>()(Home)

Trying to understand what can cause such a scenario. I have verified all the cookies are set correctly in next-firebase-auth config.

name: 'MyAppName',
      keys: [process.env.COOKIE_SECRET_CURRENT, process.env.COOKIE_SECRET_PREVIOUS],
      httpOnly: true,
      maxAge: TWELVE_DAYS_IN_MS,
      overwrite: true,
      path: '/',
      sameSite: 'lax',
      secure: config.cookieSecure,
      signed: true,