import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { admin, openAPI, username } from "better-auth/plugins";
import { passkey } from "better-auth/plugins/passkey";
import * as schema from "@/db/auth-schema";
import env from "@/env";
import { db } from "@/lib/db";
export const auth = betterAuth({
appName: env.APP_NAME,
basePath: "/api",
overrideOrigin: true,
/* database: prismaAdapter(db, {
provider: "mysql",
}), */
database: drizzleAdapter(db, {
provider: "mysql",
schema: {
...schema,
},
}),
emailAndPassword: {
enabled: true,
requireEmailVerification: true,
minPasswordLength: 8,
maxPasswordLength: 128,
autoSignIn: true,
/* sendResetPassword: async ({ _user, _url, _token }) => {
// TODO: Send reset password email
}, */
resetPasswordTokenExpiresIn: 3600,
},
advanced: {
crossSubDomainCookies: {
enabled: true,
},
defaultCookieAttributes: {
sameSite: "Lax",
secure: env.NODE_ENV === "production",
partitioned: true,
},
},
session: {
cookieCache: {
enabled: true,
maxAge: 5 * 60,
},
},
account: {
accountLinking: {
enabled: true,
trustedProviders: [],
allowDifferentEmails: false,
},
},
rateLimit: {
enabled: true,
window: 10,
max: 100,
storage: "memory",
modelName: "rate_limit",
},
onAPIError: {
throw: true,
onError: (error, _ctx) => {
console.error("Auth API Error", error);
},
errorURL: "/auth/error",
},
plugins: [
openAPI(),
],
});
let _schema: ReturnType<typeof auth.api.generateOpenAPISchema>;
const getSchema = async () => (_schema ??= auth.api.generateOpenAPISchema());
export const OpenAPI = {
getPaths: (prefix = "/auth/api") =>
getSchema().then(({ paths }) => {
const reference: typeof paths = Object.create(null);
for (const path of Object.keys(paths)) {
const key = prefix + path;
reference[key] = paths[path];
for (const method of Object.keys(paths[path])) {
const operation = (reference[key] as any)[method];
operation.tags = ["Better Auth"];
}
}
return reference;
}) as Promise<any>,
components: getSchema().then(({ components }) => components) as Promise<any>,
} as const;
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { admin, openAPI, username } from "better-auth/plugins";
import { passkey } from "better-auth/plugins/passkey";
import * as schema from "@/db/auth-schema";
import env from "@/env";
import { db } from "@/lib/db";
export const auth = betterAuth({
appName: env.APP_NAME,
basePath: "/api",
overrideOrigin: true,
/* database: prismaAdapter(db, {
provider: "mysql",
}), */
database: drizzleAdapter(db, {
provider: "mysql",
schema: {
...schema,
},
}),
emailAndPassword: {
enabled: true,
requireEmailVerification: true,
minPasswordLength: 8,
maxPasswordLength: 128,
autoSignIn: true,
/* sendResetPassword: async ({ _user, _url, _token }) => {
// TODO: Send reset password email
}, */
resetPasswordTokenExpiresIn: 3600,
},
advanced: {
crossSubDomainCookies: {
enabled: true,
},
defaultCookieAttributes: {
sameSite: "Lax",
secure: env.NODE_ENV === "production",
partitioned: true,
},
},
session: {
cookieCache: {
enabled: true,
maxAge: 5 * 60,
},
},
account: {
accountLinking: {
enabled: true,
trustedProviders: [],
allowDifferentEmails: false,
},
},
rateLimit: {
enabled: true,
window: 10,
max: 100,
storage: "memory",
modelName: "rate_limit",
},
onAPIError: {
throw: true,
onError: (error, _ctx) => {
console.error("Auth API Error", error);
},
errorURL: "/auth/error",
},
plugins: [
openAPI(),
],
});
let _schema: ReturnType<typeof auth.api.generateOpenAPISchema>;
const getSchema = async () => (_schema ??= auth.api.generateOpenAPISchema());
export const OpenAPI = {
getPaths: (prefix = "/auth/api") =>
getSchema().then(({ paths }) => {
const reference: typeof paths = Object.create(null);
for (const path of Object.keys(paths)) {
const key = prefix + path;
reference[key] = paths[path];
for (const method of Object.keys(paths[path])) {
const operation = (reference[key] as any)[method];
operation.tags = ["Better Auth"];
}
}
return reference;
}) as Promise<any>,
components: getSchema().then(({ components }) => components) as Promise<any>,
} as const;