Sahag98S
Convex Community15mo ago
4 replies
Sahag98

GetUserIdentity null

Hi there, I have a list of queries that run for it's corresponding page in my nextjs 14 web app. Inside each query I have a getUser hook that will check the identity and return the user associated with it:

import { QueryCtx } from "@/convex/_generated/server";

export async function GetUser(ctx: QueryCtx) {
  const identity = await ctx.auth.getUserIdentity();

  if (!identity) {
    throw new Error("Unauthenticated call to mutation");
  }
  const user = await ctx.db
    .query("users")
    .withIndex("byExternalId", (q) => q.eq("externalId", identity.subject))
    .unique();
  if (!user) {
    throw new Error("Unauthenticated user call");
  }

  return user;
}

However there are instances where the identity is sometimes null, therefore throwing that error and interrupting the user. How can I implement this in a better way? I feel like I'm doing something wrong here... Thank you!
Was this page helpful?