Dan Mercer
Dan Mercerβ€’2y ago

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]);
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:
No description
6 Replies
Michal Srb
Michal Srbβ€’2y ago
Hey @Dan Mercer , first to double check, are you on the latest 0.12.2 version of convex?
Dan Mercer
Dan MercerOPβ€’2y ago
I'm on 0.12.1, lemme try upgrading
Dan Mercer
Dan MercerOPβ€’2y ago
Also I tried checking isLoading too but no dice:
No description
Michal Srb
Michal Srbβ€’2y ago
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.
Dan Mercer
Dan MercerOPβ€’2y ago
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 πŸ˜‚
Michal Srb
Michal Srbβ€’2y ago
Auth is really tough. We’re working on making it better but we’re really 1% done πŸ™‚

Did you find this page helpful?