diff --git a/packages/better-auth/src/db/get-tables.test.ts b/packages/better-auth/src/db/get-tables.test.ts new file mode 100644 index 0000000000..042439ea0e --- /dev/null +++ b/packages/better-auth/src/db/get-tables.test.ts @@ -0,0 +1,60 @@ +import { describe, expect, it } from "vitest"; +import { getAuthTables } from "./get-tables"; + +describe("getAuthTables", () => { + it("should use correct field name for refreshTokenExpiresAt", () => { + const tables = getAuthTables({ + account: { + fields: { + refreshTokenExpiresAt: "custom_refresh_token_expires_at", + }, + }, + }); + + const accountTable = tables.account; + const refreshTokenExpiresAtField = + accountTable.fields.refreshTokenExpiresAt; + + expect(refreshTokenExpiresAtField.fieldName).toBe( + "custom_refresh_token_expires_at", + ); + }); + + it("should not use accessTokenExpiresAt field name for refreshTokenExpiresAt", () => { + const tables = getAuthTables({ + account: { + fields: { + accessTokenExpiresAt: "custom_access_token_expires_at", + refreshTokenExpiresAt: "custom_refresh_token_expires_at", + }, + }, + }); + + const accountTable = tables.account; + const refreshTokenExpiresAtField = + accountTable.fields.refreshTokenExpiresAt; + const accessTokenExpiresAtField = accountTable.fields.accessTokenExpiresAt; + + expect(refreshTokenExpiresAtField.fieldName).toBe( + "custom_refresh_token_expires_at", + ); + expect(accessTokenExpiresAtField.fieldName).toBe( + "custom_access_token_expires_at", + ); + expect(refreshTokenExpiresAtField.fieldName).not.toBe( + accessTokenExpiresAtField.fieldName, + ); + }); + + it("should use default field names when no custom names provided", () => { + const tables = getAuthTables({}); + + const accountTable = tables.account; + const refreshTokenExpiresAtField = + accountTable.fields.refreshTokenExpiresAt; + const accessTokenExpiresAtField = accountTable.fields.accessTokenExpiresAt; + + expect(refreshTokenExpiresAtField.fieldName).toBe("refreshTokenExpiresAt"); + expect(accessTokenExpiresAtField.fieldName).toBe("accessTokenExpiresAt"); + }); +}); diff --git a/packages/better-auth/src/db/get-tables.ts b/packages/better-auth/src/db/get-tables.ts index f294ec788d..4a416c942d 100644 --- a/packages/better-auth/src/db/get-tables.ts +++ b/packages/better-auth/src/db/get-tables.ts @@ -225,7 +225,7 @@ export const getAuthTables = ( type: "date", required: false, fieldName: - options.account?.fields?.accessTokenExpiresAt || + options.account?.fields?.refreshTokenExpiresAt || "refreshTokenExpiresAt", }, scope: {