Auth error while using useQuery
Hello Convex users and Team,
My setup is Convex with Clerk authentication. I have a sign-up rpocess after which the use is being redirected to
/rules
page, inside which I have this piece of code:
const agreedCheck = useQuery(api.users.readUserById)
.
I am also using Next.js, which shows me the error, that the Convex function, getUserIdentity
doesn't return any logged in users, although i followed every setup piece on both Convex and Clerk, so after sign-up there must be at least one user logged in, according to Clerk.
The error comes up even after refreshing the page or after redirecting the user from the sign-up page or after signing out with Clerk's Userbutton component(afterSignOutUrl="/"
).
My question is, that how should i use this Hook? is it called even when being defined as before I mentioned it, at least it seems that the useQuery hook calls my Convex function as early as being defined, like this:
const agreedCheck = useQuery(api.users.readUserById)
.
How can i solve this?2 Replies
Hey, so if you do have a correct Auth setup, there will still be a period of time when the Clerk clientside SDK is loading the auth token.
For this reason you should use these Convex-provided components:
https://docs.convex.dev/auth/clerk#logged-in-and-logged-out-views
You can wrap your rules page in <Authenticated />, and then it will only render if the auth is set up correctly.
Convex Clerk | Convex Developer Hub
Clerk is an authentication platform providing login via
Convex Clerk | Convex Developer Hub
Clerk is an authentication platform providing login via
I see, why haven't I thought of this. I guess this will solve the issue, thank you @Michal Srb