Skip to content

Custom field name panic #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
kdcokenny opened this issue Mar 12, 2025 · 1 comment · May be fixed by #33
Open

Custom field name panic #31

kdcokenny opened this issue Mar 12, 2025 · 1 comment · May be fixed by #33
Assignees

Comments

@kdcokenny
Copy link

kdcokenny commented Mar 12, 2025

Bug Report: emailHarmony plugin causes error with missing normalized_email field

Describe the bug

When adding the emailHarmony() plugin to the plugins array, the application fails. The error occurs during user creation even though the database schema includes the normalized_email column and it's properly mapped in the configuration.

To Reproduce

  1. Set up a project with better-auth and a SQLite database (in my case D1)
  2. Create database migrations that include the normalized_email field on the users table
  3. Configure better-auth with proper field mappings including normalizedEmail: "normalized_email"
  4. Add the emailHarmony() plugin to the plugins array
  5. Attempt to register/create a user
  6. Observe the error

Expected behavior

The application should successfully create users with the normalized email populated by the emailHarmony plugin.

Code Example

// Auth client configuration
const auth = betterAuth({
  database: /* database connection */,
  user: {
    modelName: "users_table",
    fields: {
      // This mapping exists but seems to be ignored
      normalizedEmail: "normalized_email",
      // Other field mappings...
    },
  },
  // Other config...
  plugins: [
    emailHarmony(), // Adding this plugin causes the error
    // Other plugins...
  ],
});

Database schema

-- Users table with normalized_email column
CREATE TABLE users_table (
  id TEXT PRIMARY KEY,
  email TEXT UNIQUE,
  normalized_email TEXT,
  -- Other columns...
);

-- Unique index on normalized_email
CREATE UNIQUE INDEX idx_users_normalized_email ON users_table(normalized_email);

Error Details

The error appears to be related to database operations when the emailHarmony plugin is active. Execution fails during the user creation process in the Kysely query builder execution:

at async InsertQueryBuilder.execute (file:///node_modules/kysely/dist/esm/query-builder/insert-query-builder.js:916:24)
at async InsertQueryBuilder.executeTakeFirst (file:///node_modules/kysely/dist/esm/query-builder/insert-query-builder.js:934:26)
at async withReturning (file:///node_modules/better-auth/dist/shared/better-auth.HHZU10im.mjs:254:13)
at async Object.create (file:///node_modules/better-auth/dist/shared/better-auth.HHZU10im.mjs:277:9)

Environment

  • better-auth: ^1.2.3
  • better-auth-harmony: ^1.2.3
  • kysely-d1: ^0.3.0
  • Node.js: (v23.7.0
  • Database: SQLite (D1)

Additional context

I've verified that:

  1. The database schema contains the normalized_email column
  2. The field is correctly mapped in the configuration
  3. Everything works until the emailHarmony() plugin is added
@kdcokenny
Copy link
Author

Update: Found the exact issue - Column naming is hardcoded

I've identified a clear reproduction of the issue. The plugin appears to ignore the field mappings entirely.

When I modified my migration to use camelCase naming directly in the database (instead of snake_case with mappings):

-- Migration: 0003_add_normalized_email.sql
-- Description: Add normalized_email field to firestorm_users table for email harmony

PRAGMA foreign_keys = ON;

-- Add normalized_email column to firestorm_users table
ALTER TABLE firestorm_users ADD COLUMN normalizedEmail TEXT;

-- Create a unique index for the normalized_email column
CREATE UNIQUE INDEX idx_users_normalized_email ON firestorm_users(normalizedEmail);

The application works perfectly without changing any of the mappings in the configuration.

This confirms that the emailHarmony() plugin is ignoring the field mappings entirely and directly expecting a column named normalizedEmail in the database, rather than using the provided mapping:

user: {
  modelName: "firestorm_users",
  fields: {
    // This mapping is being completely ignored by the plugin
    normalizedEmail: "normalized_email" 
  },
}

The plugin should respect the field mappings defined in the configuration rather than assuming database column names.

kdcokenny added a commit to kdcokenny/better-auth-harmony that referenced this issue Mar 12, 2025
Make the normalized email field name configurable via normalizedEmailField
option to fix compatibility with databases that use different field names.

Fixes GeKorm#31
@kdcokenny kdcokenny linked a pull request Mar 12, 2025 that will close this issue
@GeKorm GeKorm self-assigned this Mar 14, 2025
GeKorm added a commit that referenced this issue Mar 17, 2025
GeKorm added a commit that referenced this issue Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants