Skip to content
This repository was archived by the owner on Sep 3, 2025. It is now read-only.
This repository was archived by the owner on Sep 3, 2025. It is now read-only.

Better Auth with Convex throwing errors #24

@jacobsamo

Description

@jacobsamo

I have been slowly migrating over to convex the past couple of days and got up to migrating better auth across, it seemed to be working fine in beging but not throwing errors:

 ○ Compiling /auth/sign-in ...
 ⨯ ../../packages/backend/convex/_generated/api.d.ts
Module parse failed: Unexpected token (11:12)
|  */
|
> import type * as betterAuth from "../betterAuth.js";
| import type * as helpers from "../helpers.js";
| import type * as maps_collections from "../maps/collections.js";

Import trace for requested module:
../../packages/backend/convex/_generated/api.d.ts
./src/components/layouts/auth-flow/profile-setup.tsx
./src/components/layouts/auth-flow/index.tsx
./src/app/auth/sign-in/page.tsx
 ⨯ ../../packages/backend/convex/_generated/api.d.ts
Module parse failed: Unexpected token (11:12)
|  */
|
> import type * as betterAuth from "../betterAuth.js";
| import type * as helpers from "../helpers.js";
| import type * as maps_collections from "../maps/collections.js";

Import trace for requested module:
../../packages/backend/convex/_generated/api.d.ts
./src/components/layouts/auth-flow/profile-setup.tsx
./src/components/layouts/auth-flow/index.tsx
./src/app/auth/sign-in/page.tsx
 ⨯ ../../packages/backend/convex/_generated/api.d.ts
Module parse failed: Unexpected token (11:12)
|  */
|
> import type * as betterAuth from "../betterAuth.js";
| import type * as helpers from "../helpers.js";
| import type * as maps_collections from "../maps/collections.js";

Import trace for requested module:
../../packages/backend/convex/_generated/api.d.ts
./src/components/layouts/auth-flow/profile-setup.tsx
./src/components/layouts/auth-flow/index.tsx
./src/app/auth/sign-in/page.tsx
 GET /auth/sign-in 500 in 12378ms

I think it could do with my setup a little but unsure, I am the convex-helpers package to convert zod schemas into usable convex schemas as you can see here:

// zod-schemas/auth-schema.ts
import { zid } from "convex-helpers/server/zod";
import * as z from "zod";
import { defaultFields, insertSchema } from "./shared-schemas";

export const userSchema = z.object({
  ...defaultFields,
  name: z.string(),
  email: z.string(),
  emailVerified: z.boolean(),
  image: z.string().optional(),
  updatedAt: z.number().optional(),
  createdAt: z.number().optional(),
  twoFactorEnabled: z.boolean().optional(),
  role: z.string().optional(),
  banned: z.boolean().optional(),
  banReason: z.string().optional(),
  banExpires: z.string().optional(),
  first_name: z.string().optional(),
  last_name: z.string().optional(),
  username: z.string().optional(),
  bio: z.string().optional(),
});

// convex/schemas.ts
import { zodToConvex } from "convex-helpers/server/zod";
import { defineSchema, defineTable } from "convex/server";
import {
   // ... rest of schema
  userSchema,
} from "../zod-schemas/auth-schema";




export default defineSchema({
   // ... rest of schema
  user: defineTable(zodToConvex(userSchema))
    .index("by_email", ["email"])
    .searchIndex("search_user", {
      searchField: "name",
      filterFields: ["email", "username"],
    }),
   // ... rest of schema
});

Along with this I have some helper functions to get the user and pass it onto the query's and mutation's

//convex/helpers.ts
import { zCustomMutation, zCustomQuery } from "convex-helpers/server/zod";
import type { Id } from "./_generated/dataModel";
import {
  type MutationCtx,
  type QueryCtx,
  mutation,
  query,
} from "./_generated/server";

async function getUser(ctx: MutationCtx | QueryCtx) {
  const identity = await ctx.auth.getUserIdentity();
  console.log("IDENTITY", identity);
  if (!identity) return null;

  const user = await ctx.db
    .query("user")
    .withIndex("by_id", (q) => q.eq("_id", identity.subject as any))
    .unique();
  if (!user) return null;

  return user;
}

export const authedMutation = zCustomMutation(mutation, {
  args: {},
  input: async (ctx, args) => {
    const user = await getUser(ctx);
    console.log("USER", user);
    if (!user) throw new Error("Unauthorized");

    return {
      ctx: {
        ...ctx,
        user,
      },
      args,
    };
  },
});

export const authedQuery = zCustomQuery(query, {
  args: {},
  input: async (ctx, args) => {
    const user = await getUser(ctx);
    console.log("USER", user);
    if (!user) throw new Error("Unauthorized");

    return { ctx: { ...ctx, user }, args };
  },
});

see attached my full files for more context, I found using zod with zodToConvex a neat way of defining everything so i can export the schemas and use it not only for types but also in form schemas and validation elsewhere.

My repo: https://github.com/jacobsamo/BuzzTrip/tree/test/convex

auth.config.txt
auth-schema.txt
betterAuth.txt
helpers.txt
schema.txt
shared-schemas.txt

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions