Create user with custom ID

I am trying to configure better-auth as an alternative to Cognito. We are trying to migrate our users over, but since Cognito doesn't allow us to access the passwords, we need to do it when our user's log in. The idea is to log in with Cognito and sign the user into the better auth database. This means that the IDs should match. Is it possible to create a user with a pre-set ID, so that the old Cognito and the new user IDs match?
4 Replies
Ping
Ping•2w ago
Yeah, you can pass a generateId function in your better-auth config: https://www.better-auth.com/docs/reference/options#advanced
Options | Better Auth
Better Auth configuration options reference.
momoneko
momonekoOP•2w ago
Is there a way to pass it the id I want from the outside? It doesn't seem to have any parameter that would allow me to do that 😦
Ping
Ping•2w ago
Yeah, you can't.
Trace Panic
Trace Panic•2w ago
if you use something like drizzle you can prevent better auth from generating id, then you create your own id
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
}),
advanced: { database: { generateId: false }},
});
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
}),
advanced: { database: { generateId: false }},
});
import { accounts } from "@/db/schema/accounts";
import { admins } from "@/db/schema/admins";
import { sessions } from "@/db/schema/sessions";
import { timestamps } from "@/db/schema/timestamps";
import { createId } from "@paralleldrive/cuid2";
import { relations } from "drizzle-orm";
import { boolean, pgEnum, pgTable, text } from "drizzle-orm/pg-core";

export const userRole = pgEnum("user_role", ["student", "teacher", "admin"]);

export const users = pgTable("users", {
id: text("id")
.primaryKey()
.$defaultFn(() => createId()),
name: text("name").notNull(),
email: text("email").unique().notNull(),
role: userRole("role").notNull(),
emailVerified: boolean("email_verified")
.notNull()
.$default(() => false),
image: text("image"),
...timestamps,
});

export const userRelations = relations(users, ({ many, one }) => ({
sessions: many(sessions),
accounts: many(accounts),
adminProfile: one(admins, {
fields: [users.id],
references: [admins.userId],
}),
}));
import { accounts } from "@/db/schema/accounts";
import { admins } from "@/db/schema/admins";
import { sessions } from "@/db/schema/sessions";
import { timestamps } from "@/db/schema/timestamps";
import { createId } from "@paralleldrive/cuid2";
import { relations } from "drizzle-orm";
import { boolean, pgEnum, pgTable, text } from "drizzle-orm/pg-core";

export const userRole = pgEnum("user_role", ["student", "teacher", "admin"]);

export const users = pgTable("users", {
id: text("id")
.primaryKey()
.$defaultFn(() => createId()),
name: text("name").notNull(),
email: text("email").unique().notNull(),
role: userRole("role").notNull(),
emailVerified: boolean("email_verified")
.notNull()
.$default(() => false),
image: text("image"),
...timestamps,
});

export const userRelations = relations(users, ({ many, one }) => ({
sessions: many(sessions),
accounts: many(accounts),
adminProfile: one(admins, {
fields: [users.id],
references: [admins.userId],
}),
}));

Did you find this page helpful?