For Sparta
For Sparta•14mo ago

ctx.auth.getUserIdentity() is returning undefined

Hey! Just started using Convex so far it's been amazing. I added the Clerk integration and I am also using the latest NextJS version(with app router). I created a query function that is getting some a list from the database. When I call it from the frontend it seems the backend doesn't have the user identity right away.
export const getCurrentUsersLists = query({
args: {},
handler: async (ctx, args) => {
const identity = await ctx?.auth?.getUserIdentity()

const info = identity?.name
console.log("🚀 ~ handler: ~ info:", info)



return "hello"
}
})
export const getCurrentUsersLists = query({
args: {},
handler: async (ctx, args) => {
const identity = await ctx?.auth?.getUserIdentity()

const info = identity?.name
console.log("🚀 ~ handler: ~ info:", info)



return "hello"
}
})
In this function I need to be able to get lists by the user id but the user is never defined. I have made sure I am logged in too. I have also made sure that I setup my auth.config.js correctly. It seems the mutation function works just fine but the queries do not. Any help would be awesome! Thanks. Here is what the logs look like. Seems like on load it's undefined and then eventually the data comes.
No description
6 Replies
Michal Srb
Michal Srb•14mo ago
Hey @For Sparta, the client needs to authenticate with the Convex server, before it does UserIdentity is indeed undefined. You can wrap your component in Authenticated to make sure it doesn't render until the client-server authentication handshake: https://docs.convex.dev/auth/clerk#logged-in-and-logged-out-views Alternatively you can return null while the auth context is loading, and handle it on the client (this is slightly faster, as it avoids an additional roundtrip).
Convex Clerk | Convex Developer Hub
Clerk is an authentication platform providing login via
For Sparta
For SpartaOP•14mo ago
Ahh yep that did it. Thank you.
const IsAuthenticated = () => {
const lists = useQuery(api.list.getLists);
return (
<div>
{lists?.map((list) => (
<div key={list._id}>{list.name}</div>
))}
</div>
);
}
const IsAuthenticated = () => {
const lists = useQuery(api.list.getLists);
return (
<div>
{lists?.map((list) => (
<div key={list._id}>{list.name}</div>
))}
</div>
);
}
<Authenticated>
<IsAuthenticated />
</Authenticated>
<Authenticated>
<IsAuthenticated />
</Authenticated>
Did the trick!
TheCyberverse1 - $PREDO
hi, This doesn't work for me in the backend as I try to get the user identity in a webhook function, but it keeps returning null
TheCyberverse1 - $PREDO
Here is what I get whenever I'm trying to get the user identity in a webhook
No description
TheCyberverse1 - $PREDO
Here is the code: export const webhookHandler = httpAction(async (ctx, request) => { const body = await request.json(); const { target, installation } = body; const userId = await getUserId(ctx); if (userId === undefined) { throw new ConvexError('User must be logged in.'); } switch (target) { case 'created': await handleInstallationCreated(ctx, installation); } return new Response(Webhook Recieved, { status: 200 }); });
erquhart
erquhart•12mo ago
@The Cyberverse yours is a different issue, I gave a reply for it here: https://discord.com/channels/1019350475847499849/1218192747882872943/1218193536323817490

Did you find this page helpful?