react-oauth icon indicating copy to clipboard operation
react-oauth copied to clipboard

How to store jwt token?

Open vabatista opened this issue 1 year ago • 0 comments

I'm trying to persist logged user between refreshes with code below in React app

 const gtoken = localStorage.getItem('g-token');
  if (gtoken !== undefined) {
    const decoded: { email: string } = jwtDecode(gtoken as string);
    setLoggedUser(decoded.email);
  } else {
    useGoogleOneTapLogin({
      onSuccess: credentialResponse => {
        console.log(credentialResponse);
        if (credentialResponse.credential) {
          const decoded: { email: string } = jwtDecode(credentialResponse.credential as string);
          localStorage.setItem('g-token', credentialResponse.credential);
          setLoggedUser(decoded.email);
        }
      },
      onError: () => {
        console.log('Login Failed');
      },
      auto_select: true
    });
  }

but this triggers an error: React Hook "useGoogleOneTapLogin" is called conditionally. React Hooks must be called in the exact same order in every component render react-hooks/rules-of-hooks

What is the best way to deal with this?

vabatista avatar Mar 14 '24 18:03 vabatista