supertokens / supertokens-auth-react

ReactJS authentication module for SuperTokens
https://supertokens.com
Other
276 stars 84 forks source link

Component wrapped in `SessionAuth` doesn't get the latest session context #731

Closed davidfant closed 1 year ago

davidfant commented 1 year ago

In the following scenario, the child won't get updates to the session context:

import { SessionAuth, useSessionContext } from 'supertokens-auth-react/recipe/session';

function Child() {
  const session = useSessionContext();
  ...
}

function Parent() {
  const session = useSessionContext();
  return (
    <SessionAuth>
      <Child />
    </SessionAuth>
  );
}

I think it's because of this, where a new SessionContext.Provider with a context stored in state is wrapping the children: https://github.com/supertokens/supertokens-auth-react/blob/5818082e6a242246edd7578362e7797cda5850e1/lib/ts/recipe/session/sessionAuth.tsx#L68-L70

My workaround for now is:

function Parent() {
  const session = useSessionContext();
  return (
    <SessionAuth>
      <SessionContext.Provider value={session}>
        <Child />
      </SessionContext.Provider>
    </SessionAuth>
  );
}
porcellus commented 1 year ago

Hi, I can't reproduce this, it seems to work OK in our examples. Could you modify one of our examples to replicate this issue and/or describe your scenario in more detail?

Also, the code line you linked in SessionAuth causes it to reload the session from the stored state in every SessionAuth instance instead of reusing values from any parent. This is useful for some of our validations, but (more importantly) it also attaches all the same event handlers that the parent has, so it should get the same updates.

porcellus commented 1 year ago

Hi, since I couldn't reproduce this, I'll close the issue, but please feel free to re-open if you can add more details and help me reproduce the issue.