mirror of
https://github.com/better-auth/better-auth.git
synced 2026-06-01 03:46:39 -05:00
fix(two-factor): wire twoFactorTable option to schema modelName (#8443)
This commit is contained in:
@@ -441,7 +441,15 @@ export const twoFactor = <O extends TwoFactorOptions>(options?: O) => {
|
||||
},
|
||||
],
|
||||
},
|
||||
schema: mergeSchema(schema, options?.schema),
|
||||
schema: mergeSchema(schema, {
|
||||
...options?.schema,
|
||||
twoFactor: {
|
||||
...options?.schema?.twoFactor,
|
||||
...(options?.twoFactorTable
|
||||
? { modelName: options.twoFactorTable }
|
||||
: {}),
|
||||
},
|
||||
}),
|
||||
rateLimit: [
|
||||
{
|
||||
pathMatcher(path) {
|
||||
|
||||
@@ -1394,6 +1394,45 @@ describe("twoFactorCookieMaxAge", async () => {
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* @see https://github.com/better-auth/better-auth/issues/8424
|
||||
*/
|
||||
describe("twoFactorTable option", async () => {
|
||||
const { auth, signInWithTestUser, testUser, db } = await getTestInstance({
|
||||
secret: DEFAULT_SECRET,
|
||||
plugins: [
|
||||
twoFactor({
|
||||
twoFactorTable: "custom_two_factor",
|
||||
skipVerificationOnEnable: true,
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
let { headers } = await signInWithTestUser();
|
||||
|
||||
it("should use custom table name for two factor data", async () => {
|
||||
const enableRes = await auth.api.enableTwoFactor({
|
||||
body: { password: testUser.password },
|
||||
headers,
|
||||
asResponse: true,
|
||||
});
|
||||
expect(enableRes.status).toBe(200);
|
||||
headers = convertSetCookieToCookie(enableRes.headers);
|
||||
|
||||
const twoFactorRecord = await db.findOne<TwoFactorTable>({
|
||||
model: "twoFactor",
|
||||
where: [
|
||||
{
|
||||
field: "userId",
|
||||
value: (await auth.api.getSession({ headers }))?.user.id as string,
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(twoFactorRecord).toBeDefined();
|
||||
expect(twoFactorRecord?.secret).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("OTP storage modes", async () => {
|
||||
describe("hashed OTP storage", async () => {
|
||||
let OTP = "";
|
||||
|
||||
@@ -10,6 +10,13 @@ export interface TwoFactorOptions {
|
||||
* Application Name
|
||||
*/
|
||||
issuer?: string | undefined;
|
||||
/**
|
||||
* The name of the table that stores the two factor
|
||||
* authentication data.
|
||||
*
|
||||
* @default "twoFactor"
|
||||
*/
|
||||
twoFactorTable?: string | undefined;
|
||||
/**
|
||||
* TOTP OPtions
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user