Hey! Wondering if anyone has had a
Hey! Wondering if anyone has had a similar issue to what I'm experiencing.
I've setup a next js application with convex and clerk. All nice and simple. Implemented the login and authenticated pages/middleware absolutely fine.
Now this is where I'm confused, when I try and read the auth inside the functions like the example stays my indentity is always null and the function throws an error. However, if I remove the if check and just log out the identity it is what's expected.
14 Replies
Unsure exactly based on your description, but is your auth.config.js file configured correctly in the convex folder?
Sorry about it the description being somewhat vague
Yes, I believe the config is configured correctly
I also believe it is because I can see the identity when I don't write any IF blocks
I'm unsure why introducing an if statement would change the behavior of the auth identity. Seems like that may be a red herring.
I completely agree, this is why I've turned to the discord because I have absolutely no understanding of what else to look at
Can you share your code where it works and where it throws?
(or returns null)
What I can see is that when I remove the if I and I request the function I get three logs
First returning null and the second two returning the identity
sounds more like a race condition to me.
how are you determining auth is ready on the client?
Oh my god..
I basically completely missed the determining the auth is ready on the client because the layout was server rendered and thus the client component that called the convex function was able to send before the "client auth" was ready
Holy moly I feel stupid, thank you so much for the help @allen
Yeah you need to not rely on the clerk status whatsoever and use the convex auth status to determine readiness. Either block the mounting of authenticated paths until its ready, or conditional defer queries with the
'skip'
argument.Yeah, I used useSession from clerk nextjs and then ensured it is loaded before I mount and client components
Its a common pitfall. Maybe the convex team can chime in on why this is, as it seems if the underlying auth provider is ready, the convex
useAuth
method for fetchAccessToken
should be able to resolve a valid token when the query is executed.Yeah, I think I got stuck behind the theory that it would be automatically available due to the provider but I somewhat understand that we should guard the client to ensure everything is ready
Ignoring SSR for now, there are several phases:
1. Client has no auth
2. Client receives JWT token from Clerk (which it passes to Convex) (
useUser
/ useSession
from clerk look done)
3. Convex server receives and accepts the JWT token, sends a ack back to the client (useConvexAuth
/ Authenticated
from Convex look done)
4. (optional) If you use either a mutation or a webhook to pass user info to Convex, there's one more step when you write the info into the DB, and now your Convex functions can read it.
With SSR the first phase could be skipped (although I'd need to go check what Clerk actually does to be sure)Ah I guess the handshake on #3 wasnt apparent to me. I figured it was done at request time.