Dan MercerD
Convex Community3y ago
8 replies
Dan Mercer

useConvexAuth().isAuthenticated is true, but auth fails

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:
image.png
Was this page helpful?