#useConvexAuth().isAuthenticated is true, but auth fails

8 messages · Page 1 of 1 (latest)

urban bison
#

To store the Auth0 user in my Convex database, I have a user/create mutation that inserts the user (if not already in the db) and then returns it. I use it like so:

  const { isAuthenticated } = useConvexAuth();

  const [convexUser, setConvexUser] = useState<User | undefined>(undefined);

  // when auth0 state changes, tell Convex and create the user
  useEffect(() => {
    console.log('useConvexAuth().isAuthenticated is', isAuthenticated);
    if (isAuthenticated) {
      convex.mutation('user/create')()
        .then((convexUser) => {
          console.log('Retrieved user record from Convex database');
          setConvexUser(convexUser);
        }, e => {
          console.warn(`Failed to get/create Convex user`, e);
          setConvexUser(undefined);
        })
    } else {
      setConvexUser(undefined);
    }
    return () => {
      console.log('clearing Convex user');
      setConvexUser(undefined);
    }
  }, [isAuthenticated]);

But the mutation fails (sometimes - consistently on my desktop, but not on my phone 🤔 ) with this error:

dusky sand
#

Hey @urban bison , first to double check, are you on the latest 0.12.2 version of convex?

urban bison
#

I'm on 0.12.1, lemme try upgrading

#

Also I tried checking isLoading too but no dice:

dusky sand
#

0.12.1 should be ok. The error would suggest to me that the client is being authenticated but the server cannot verify the token the client is passing down. I’d double check that convex.json has the right Auth0 keys and that it’s been deployed via npx convex dev.

urban bison
#

Ok weird update... clearing my localStorage (and logging in again) fixed it 🤔

#

I guess my auth0 session was messed up somehow? Thanks anyway for the help 😅

#

I feel like I have the weirdest auth problems all the time... I guess I'm just bad at using auth0 😂