#how do i get user id from auth identity

10 messages · Page 1 of 1 (latest)

thin blade
#

export const listAssignedIssues = query({
args: {},

async handler(ctx) {
    const identity = await ctx.auth.getUserIdentity();
    if (!identity) return [];

   const user = await ctx.db.query("users")
        .withIndex("by_token", (q) => q.eq("tokenIdentifier", identity.tokenIdentifier))
        .unique();

    return ctx.db.query("issues")
        .withIndex("by_assignee", (q) => q.eq("assignee", identity.subject)).collect();
},

});

agile snowBOT
#

Hi @thin blade. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

You generally don’t use identity.subject directly as your Convex "users" document _id. Instead, you:

  1. Look up (or create) the user document using something stable from the JWT (commonly tokenIdentifier).
  2. Use that user document’s _id as the foreign key in other tables. [Storing users]

So your function should be:

import { query } from "./_generated/server";

export const listAssignedIssues = query({
  args: {},
  async handler(ctx) {
    const identity = await ctx.auth.getUserIdentity();
    if (!identity) return [];

    // Look up the Convex user document by tokenIdentifier
    const user = await ctx.db
      .query("users")
      .withIndex("by_token", (q) =>
        q.eq("tokenIdentifier", identity.tokenIdentifier),
      )
      .unique();

    if (!user) {
      // User not stored yet – decide whether to throw or return []
      return [];
    }

    // Use the Convex user _id as the assignee foreign key
    return ctx.db
      .query("issues")
      .withIndex("by_assignee", (q) => q.eq("assignee", user._id))
      .collect();
  },
});

This matches the pattern used in the docs where tokenIdentifier is used to find the user, and then _id is used in other tables. [Using current user ID]

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

#

@thin blade Kapa.ai is still learning and improving, please let me know how I did by reacting below.

thin blade
#

rgument of type '"by_token"' is not assignable to parameter of type '"email" | "phone" | keyof SystemIndexe

it's not index in authTables from import { authTables } from "@convex-dev/auth/server";

#

@agile snow rgument of type '"by_token"' is not assignable to parameter of type '"email" | "phone" | keyof SystemIndexe

it's not index in authTables from import { authTables } from "@convex-dev/auth/server";

agile snowBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

You’re running into a type error because you’re mixing two different patterns:

  • Convex Auth (authTables), where Convex manages a users table for you.
  • Manual “users” table with tokenIdentifier + by_token index, as shown in the non‑Convex‑Auth docs. [Storing users]

With Convex Auth, you don’t have a tokenIdentifier field or a by_token index on the auth users table, so TypeScript correctly complains.

How to get the current user with Convex Auth

When using Convex Auth, you should use getAuthUserId instead of querying by tokenIdentifier yourself. [Use auth state]

Example:

import { query } from "./_generated/server";
import { getAuthUserId } from "@convex-dev/auth/server";

export const listAssignedIssues = query({
  args: {},
  async handler(ctx) {
    const userId = await getAuthUserId(ctx);
    if (userId === null) return [];

    // Here `assignee` should be an Id<"users"> field in your "issues" table
    return ctx.db
      .query("issues")
      .withIndex("by_assignee", (q) => q.eq("assignee", userId))
      .collect();
  },
});

Key points:

  • getAuthUserId(ctx) returns the Convex users document ID (or null if not signed in). [Use auth state]
  • Your "issues" table’s assignee field should be v.id("users"), and your by_assignee index should be defined on that field.
  • You no longer need a by_token index or a tokenIdentifier field when using Convex Auth’s authTables.
#

If you do want a separate custom "users" table with tokenIdentifier and by_token, that table must be defined in your own schema.ts (not in authTables), and you’d query that table instead of the Convex Auth users table. But the knowledge sources don’t show combining those two patterns, so I can’t give a precise schema for that hybrid setup.

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