nhost: Next JWT token expired when using SSR

So after discussion with @dbarrosop in discord help channel I’ve decided to open an Issue here

Describe the bug I’m investigation integration next + nhost with SSR enabled (pages router). I’ve setup our app the same way as in examples - <NhostProvider> + <NhostApolloProvider> + getNhostSession from nhost/nextjs

In general it works really well! Thanks guys for all your work.

But sometimes (and it’s even not clear when and why) I recieve errors about JWT token being expired, both on client and server. Previously we were using next but without getNhostSessions so all auth part was working only on client and it was working flawlessly

error: ApolloError: Could not verify JWT: JWTExpired To Reproduce Steps to reproduce the behavior:

  1. Go to start page.
  2. Wait till refresh token expired

Expected behavior Token updates automatically

Screenshots

Desktop (please complete the following information):

Code

ApptProvider in _app.tsx

export const Provider: FC<ProviderProps> = ({
  children,
  nhostSession,
  transitionConfig = {
    type: 'spring',
    bounce: 0.01,
  },
}) => {
  return (
    <NhostProvider nhost={nhost} initial={nhostSession}>
      <ApolloProvider>
        <MotionConfig transition={transitionConfig}>{children}</MotionConfig>
      </ApolloProvider>
    </NhostProvider>
  );
};

ApolloProvider used above:

import { FC, PropsWithChildren } from 'react';
import { NhostApolloProvider } from '@nhost/react-apollo';
import { nhost } from '@htch/shared-services/nhost';
import { NhostApolloClientOptions } from '@nhost/apollo';

export const ApolloProvider: FC<
  PropsWithChildren<Omit<NhostApolloClientOptions, 'nhost'>>
> = ({ children, ...props }) => {
  return (
    <NhostApolloProvider nhost={nhost} {...props}>
      {children}
    </NhostApolloProvider>
  );
};

getNhostSession:

import { getNhostSession as fetchNhostSession } from '@nhost/nextjs';
import { REGION, SUBDOMAIN } from '@htch/shared-services/nhost';
import { GetServerSidePropsContext } from 'next';

export const getNhostSession = (context: GetServerSidePropsContext) =>
  fetchNhostSession(
    {
      subdomain: SUBDOMAIN,
      region: REGION,
    },
    context
  );

Screeshoots

image

this is console.log from getNhostSession image

So session is here, but JWT is expired

Nhost refresh token expired at looks correct image So it expires in a future at the moment of screenshot

Also noticed that If I delete nhostSession from cookies and just refersh page - it started to work

Would be glad to get any help with it. Thanks!

About this issue

  • Original URL
  • State: closed
  • Created 8 months ago
  • Reactions: 1
  • Comments: 24 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Not yet, unfortunately December turned out to be way too optimistic, specially given our AI week and the holidays. We will most likely start looking into this before the end of the month.

We just started looking into this so we will hopefully have news soon.

HNY folks!

Curious: did you get to look at this in Dec?

We see the often over and over again in production.

Unfortunately we are a bit caught up with other stuff right now but it’s on our shortlist of things to look at so as soon as those are out of the way this comes next (should be at some point during December 🤞). Apologies for the delay.

The issue is that we haven’t been able to reproduce consistently so any information you can share on how to reproduce will help debug this. For instance, when does it happen? Is this an issue with users having the refresh token expire? Do you have subscriptions?

As you probably noticed we have a fix already. We will be releasing a new version of the SDKs with it tomorrow. If you upgrade, we’d appreciate if you could confirm the issue is gone. Thanks for your patience.

Yep, it’s quite hard to reproduce. So I’ve noticed that this happens in dev env (next 13.3 + ssr + page routes) when I have two users simultaneously accessing application. Looks like it’s same issue on our dev env with production build.

Yes, we do have subscriptions and we use them quite a lot. For me it looks like token expired and client on server doesn’t know how to refresh it and after this things are messed up. The only thing that helps - is to do signOut and then sign in back. Just refresh page and trigger new SSR run does not help.

Maybe I can take a look at some specific logs when we have this error and provide it to you?

thanks for the report, we will try to look into this as soon as possible