I am consistently having an issue with an error: `...
# help
s
I am consistently having an issue with an error:
JWT Expired
if I'm using the app for over an hour. This is the hook being used to attempt to update the user credentials as needed, but it doesn't work. Am I doing something wrong?
Copy code
js
import { supabase } from '@root/services/supabase';
import { useSignIn } from '@root/api/auth/mutations';
import { useAuthState } from '@root/state/useAuthState';
import { useMount } from 'react-use';

export const useUpdateOnAuthStateChange = () => {
  const { session: localSession, setSession, clearSession } = useAuthState();
  const { mutate: signIn } = useSignIn({
    onError: (error) => console.error(error),
  });

  useMount(() => {
    if (localSession?.refresh_token) {
      signIn({ refreshToken: localSession.refresh_token });
    }
  });

  useMount(() => {
    setSession(supabase.auth.session());
    supabase.auth.onAuthStateChange((_event, session) => {
      console.log('_event :>> ', _event);
      console.log('session :>> ', session);
      if (!!session) {
        setSession(session);
      } else {
        clearSession();
      }
    });
    window.onstorage = (e) => {
      if (e.key === 'supabase.auth.token') {
        const newSession = JSON.parse(e.newValue);
        setSession(newSession?.currentSession);
      }
    };
  });
};
n
Hello @stnmonroe! This thread has been automatically created from your message in #843999948717555735 a ``few seconds ago``. Pinging @User so that they see this as well! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ... menu) and select Leave Thread to unsubscribe from future updates. Want to change the title? Use the
/title
command! We have solved your problem? Click the button below to archive it.
s
Also, if I refresh, it's good to go, but this is an app that a user may have open for longer amounts of time than an hour.
b
Well to start off with why are you using some random useMount thing
n
stnmonroe (2022-04-18)
b
What's wrong with useEffect
s
useMount
is a
useEffect
without a dependency array. When developing, you should use things that promote intentionality. As a team grows, someone may add a dependency to a
useEffect
not understanding that the intention was always to be a
useMount
. That is why. Anyway, with Firebase I've never had an issue with this, but with Supabase it's consistent. Any idea what I'm doing wrong?
Should I be using the
refresh_token
in this way? It originally wasn't there, but was added in an attempt to debug. How do I auto refresh the user session when the token expires without having to refresh the entire page?
Thanks in advance @barry
Hey @barry ... Is there anyone else I should talk to about this issue?
b