Open SlavenIvanov opened 1 month ago
Getting the same issue with the default function being ignored in the drizzle schema and a UUID being generated regardless. Is there any work around for this?
I'm currently using mysql2 with drizzle orm combined with the resend provider. I've noticed that when the email is sent for the magic link in the signIn callback prior to sending the magic link on new user, the user object is populated and has an id of uuid.
I've pulled down the source code for this lib and i can't see where that new user object is generated being passed in, however i have managed to resolve the issue.
My original next auth config looked like the following:
export const { handlers, signIn, signOut, auth } = NextAuth({
debug: true,
session: { strategy: "jwt" },
providers: [Resend({ from: "no-reply@****" })],
adapter: DrizzleAdapter(db),`
import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2";
import * as schema from "@/schema";
const connection = mysql.createConnection({
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
});
export const db = drizzle(connection, {
schema,
mode: "default",
});
I assumed that exporting the schema with the updated schema would have used my custom drizzle schema that uses a default fuction to generate a cuid2 rather than the uuid.
However it does not honor this.
I had to change the config to explicitly state the custom drizzle schemas for each table as follows:
import { DrizzleAdapter } from "@auth/drizzle-adapter";
import { eq } from "drizzle-orm";
import { userInfo, users } from "./schema";
import NextAuth, { AuthError } from "next-auth";
import Resend from "next-auth/providers/resend";
import * as schema from "@/schema";
export const { handlers, signIn, signOut, auth } = NextAuth({
debug: true,
session: { strategy: "jwt" },
providers: [Resend({ from: "no-reply@***" })],
adapter: DrizzleAdapter(db, {
usersTable: schema.users,
accountsTable: schema.accounts,
authenticatorsTable: schema.authenticators,
sessionsTable: schema.sessions,
verificationTokensTable: schema.verificationTokens,
}),
After changing to this it creates a new user honoring the default function and creates the CUID2 as a wanted.
It would appear that without doing this the default mysql.ts schema is used to create the user
Adapter type
@auth/drizzle-adapter
Environment
How to reproduce
npm i
docker-compose up -d
npm run drizzle:push
npm run dev
👉 pageAppnpm run drizzle:studio
and open drizzle studio 👉 pageDrizzleuser
tableThe Id should be a
nanoid()
but is auuid
And we know that the drizzle schema is correct because if you create a new user manually:The id is correctly picking up the
nanoid()
Expected behavior
DrizzleKit Adapter should call the
$default()
/$defaultFn()
instead of generating a uuid for theuser.id