#User ID

3 messages · Page 1 of 1 (latest)

bleak bone
#

How do i get the user ID of the person that is logged in with Convex AUTH?

vernal fogBOT
#

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.

For more details, see the official documentation: Get currently signed-in user ID and getAuthUserId API reference.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: