ibrahimyaacob
ibrahimyaacob8mo ago

how to use getUserIdentity with fetchQuery (server component)

getUserIdentity returns null when working this way
7 Replies
Michal Srb
Michal Srb8mo ago
Next.js Server Rendering | Convex Developer Hub
Next.js automatically renders both Client and Server Components on the server
ibrahimyaacob
ibrahimyaacobOP8mo ago
my bad, but thanks!
hugo
hugo7mo ago
Hi @Michal Srb , I'm using this repo: https://github1s.com/xixixao/convex-auth/tree/convex-auth-lib to implement auth. I use fetchQuery to get user data from a server component always return null. The link you've provided above required an access token to validate, but how to get that from this repo or to create a new one. Thanks
No description
panzacoder
panzacoder7mo ago
@hugo the token has to come from you auth provider. When you are using useQuery convex automatically grabs it from the client, so you don't have to manually pass your token. With fetchQuery you have to manually pass the token prop. You can get this from your auth provider, in my case that's Clerk:
import { auth } from '@clerk/nextjs/server'

export async function getAuthToken() {
return (await auth().getToken({ template: 'convex' })) ?? undefined
}
import { auth } from '@clerk/nextjs/server'

export async function getAuthToken() {
return (await auth().getToken({ template: 'convex' })) ?? undefined
}
I re-use that function any time I need to fetch. You could also create a function that wraps fetchQuery for fetchAuthQuery that injects the token
Michal Srb
Michal Srb7mo ago
Hey @hugo, that code is old. The library I'm working on is still WIP. To your question, the library will have a separate Next.js integration.
hugo
hugo7mo ago
thank you
Michal Srb
Michal Srb7mo ago
@hugo The Next.js integration is not out yet but the library is, see https://docs.convex.dev/auth/convex-auth
Convex Auth | Convex Developer Hub
Convex Auth is a library for implementing authentication directly within your

Did you find this page helpful?