#User ID
3 messages · Page 1 of 1 (latest)
Hi @bleak bone. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
To get the user ID of the currently logged-in user with Convex Auth, use the getAuthUserId function from @convex-dev/auth/server inside your Convex query, mutation, or action. Here’s how you do it:
import { getAuthUserId } from "@convex-dev/auth/server";
import { query } from "./_generated/server";
export const currentUser = query({
args: {},
handler: async (ctx) => {
const userId = await getAuthUserId(ctx);
if (userId === null) {
// User is not authenticated
return null;
}
// userId is the document ID of the user in the "users" table
return userId;
},
});
- getAuthUserId(ctx) returns the user’s document ID from the users table, or null if the client isn’t authenticated.
- This function is specific to Convex Auth and should not be used with other providers like Clerk or Auth0.