colinhacks / next-firebase-ssr

An Next.js example repo for building authenticated pages with Firebase Authentication, cookies, and getServerSideProps
https://vriad.com/essays/nextjs-firebase-authentication
424 stars 61 forks source link

Redirect on first load of authenticated page #19

Open davidwitten opened 3 years ago

davidwitten commented 3 years ago

I followed the example and redirected when the page isn't authenticated: https://github.com/colinhacks/next-firebase-ssr/blob/master/pages/authenticated.tsx#L25

As a result, when I load my website for the first time in a while, it redirects to /404. When I reload my authenticated page it works (bc the website regenerates my token).

Is there any way to resolve this? What's the preferred way to refresh your token?

This is my full error message:

ERROR:  FirebaseAuthError: Firebase ID token has expired. Get a fresh ID token from your client app and try again (auth/id-token-expired). See https://firebase.google.com/docs/auth/admin/verify-id-tokens for details on how to retrieve an ID token.
    at FirebaseAuthError.FirebaseError [as constructor] (error.js:44:28)
    at FirebaseAuthError.PrefixedFirebaseError [as constructor] (error.js:90:28)
    at new FirebaseAuthError (error.js:149:16)
    at FirebaseTokenVerifier.mapJwtErrorToAuthError (token-verifier.js:223:20)
    at token-verifier.js:207:25
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async getServerSideProps (authenticated_page.js:29:19)
    at async Object.renderToHTML (render.js:473:24)
    at async doRender (next-server.js:1127:38) {
  errorInfo: {
    code: 'auth/id-token-expired',
    message: 'Firebase ID token has expired. Get a fresh ID token from your client app and try again (auth/id-token-expired). See https://firebase.google.com/docs/auth/admin/verify-id-tokens for details on how to retrieve an ID token.'
  },
  codePrefix: 'auth'
}
Nithur-M commented 2 years ago

Having the same issue.