Skip to content

auth.api.getSession().user doesn't return BetterAuth component generated type Doc<"user"> #120

@brianwk

Description

@brianwk

I have the following helper function:

export const getCurrentUser = async (
  ctx: GenericQueryCtx<DataModel> | GenericMutationCtx<DataModel>,
  auth: ReturnType<typeof createAuth>
): Promise<AuthDoc<"user">> => {
  const session = await auth.api.getSession({
    //baseURL: process.env.NEXT_PUBLIC_BASE_URL,
    headers: await authComponent.getHeaders(ctx),
  });
  console.log("Session response:", session);
  if (!session) {
    throw new ConvexError("Not authenticated");
  }
  return session.user;
};

On the last line where session.user is returned I get an error: Type '{ id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; username: string; onboardingStep?: number | null | undefined; ... 8 more ...; banExpires?: Date | ... 1 more ... | undefined; }' is missing the following properties from type '{ _id: Id<"user">; _creationTime: number; image?: string | null | undefined; userId?: string | null | undefined; role?: string | null | undefined; banned?: boolean | null | undefined; ... 12 more ...; username: string; }': _id, _creationTime ts(2739)

BetterAuth User type seems to lack _id and _creationTime fields.

By the way, I am using a local install of the BetterAuth Convex component. If I change the return type to:

Promise<
  Omit<
    AuthDoc<"user">,
    "_id" | "_creationTime" | "createdAt" | "updatedAt" | "banExpires"
  >
>

it gets rid of the type error. However, it does seem to defeat the purpose of the generated types in Convex.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions