not inserted into database prisma

i added additional fields but it doesnt seems to saved in db
6 Replies
mishu
mishuOP3mo ago
export const auth = (environment: CloudflareBindings) => {
const prisma = getPrisma(environment)

return betterAuth({
database: prismaAdapter(prisma, { provider: 'postgresql' }),
baseURL: environment.BETTER_AUTH_URL,
secret: environment.BETTER_AUTH_SECRET,
user: {
additionalFields: {
role: {
type: 'string[]',
required: true,
defaultValue: ['HUSTLR'],
input: false
},
gender: {
type: 'string',
required: true,
defaultValue: '',
input: true
},
phone: {
type: 'string',
required: false,
defaultValue: '',
input: true
}
}
},
emailAndPassword: {
enabled: true,
password: {
async hash(password) {
const salt = randomBytes(16)
const hash = pbkdf2Sync(password, salt, 1000, 32, 'sha256')

return `${salt.toString('hex')}:${hash.toString('hex')}`
},
async verify({ hash, password }) {
const [saltHex, storedHashHex] = hash.split(':')
const salt = Buffer.from(saltHex, 'hex')
const storedHash = Buffer.from(storedHashHex, 'hex')

const testHash = pbkdf2Sync(password, salt, 1000, 32, 'sha256')
return storedHash.equals(testHash)
}
}
},
plugins: [openAPI()],
databaseHooks: {
user: {
create: {
async before(user) {
return { data: { ...user, phone: 'not inserted' } }
}
}
}
}
})
}
export const auth = (environment: CloudflareBindings) => {
const prisma = getPrisma(environment)

return betterAuth({
database: prismaAdapter(prisma, { provider: 'postgresql' }),
baseURL: environment.BETTER_AUTH_URL,
secret: environment.BETTER_AUTH_SECRET,
user: {
additionalFields: {
role: {
type: 'string[]',
required: true,
defaultValue: ['HUSTLR'],
input: false
},
gender: {
type: 'string',
required: true,
defaultValue: '',
input: true
},
phone: {
type: 'string',
required: false,
defaultValue: '',
input: true
}
}
},
emailAndPassword: {
enabled: true,
password: {
async hash(password) {
const salt = randomBytes(16)
const hash = pbkdf2Sync(password, salt, 1000, 32, 'sha256')

return `${salt.toString('hex')}:${hash.toString('hex')}`
},
async verify({ hash, password }) {
const [saltHex, storedHashHex] = hash.split(':')
const salt = Buffer.from(saltHex, 'hex')
const storedHash = Buffer.from(storedHashHex, 'hex')

const testHash = pbkdf2Sync(password, salt, 1000, 32, 'sha256')
return storedHash.equals(testHash)
}
}
},
plugins: [openAPI()],
databaseHooks: {
user: {
create: {
async before(user) {
return { data: { ...user, phone: 'not inserted' } }
}
}
}
}
})
}
model user {
id String @id
name String
email String @unique
emailVerified Boolean @default(false)
image String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
role ROLE[] @default([HUSTLR])
gender GENDER?
phone String?

session session[]
account account[]
}
model user {
id String @id
name String
email String @unique
emailVerified Boolean @default(false)
image String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
role ROLE[] @default([HUSTLR])
gender GENDER?
phone String?

session session[]
account account[]
}
mishu
mishuOP3mo ago
No description
mishu
mishuOP3mo ago
@Better Auth can u help me?
Better Auth
Better Auth3mo ago
Hey there! I'm here to help with anything related to Better Auth. Could you let me know what you're having trouble with? Whether it's installation (using commands like npx @better-auth/cli migrate [1]), configuring social providers, or trouble with your baseURL setup (as seen with some issues in our backend logs [7]), feel free to share more details so I can give you a specific answer. Sources: [1] Installation | Better Auth [7] better-auth works with the wrong baseURL
GitHub
better-auth/better-auth
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
Installation | Better Auth
Learn how to configure Better Auth in your project.
GitHub
better-auth works with the wrong baseURL · Issue #3215 · better-a...
Is this suited for github? Yes, this is suited for github To Reproduce I have an express backend served under the https://app.mydomain.org/api/ route that runs better-auth. I configure this with th...
mishu
mishuOP3mo ago
@Better Auth this is my config however its not inserted in database why
export const auth = (environment: CloudflareBindings) => {
const prisma = getPrisma(environment)

return betterAuth({
database: prismaAdapter(prisma, { provider: 'postgresql' }),
baseURL: environment.BETTER_AUTH_URL,
secret: environment.BETTER_AUTH_SECRET,
user: {
additionalFields: {
role: {
type: 'string[]',
required: true,
defaultValue: ['HUSTLR'],
input: false
},
gender: {
type: 'string',
required: true,
defaultValue: '',
input: true
},
phone: {
type: 'string',
required: false,
defaultValue: '',
input: true
}
}
},
databaseHooks: {
user: {
create: {
async before(user) {
return { data: { ...user, phone: 'not inserted' } }
}
}
}
}
})
}
export const auth = (environment: CloudflareBindings) => {
const prisma = getPrisma(environment)

return betterAuth({
database: prismaAdapter(prisma, { provider: 'postgresql' }),
baseURL: environment.BETTER_AUTH_URL,
secret: environment.BETTER_AUTH_SECRET,
user: {
additionalFields: {
role: {
type: 'string[]',
required: true,
defaultValue: ['HUSTLR'],
input: false
},
gender: {
type: 'string',
required: true,
defaultValue: '',
input: true
},
phone: {
type: 'string',
required: false,
defaultValue: '',
input: true
}
}
},
databaseHooks: {
user: {
create: {
async before(user) {
return { data: { ...user, phone: 'not inserted' } }
}
}
}
}
})
}
curl --location 'http://localhost:8787/api/auth/sign-up/email' \ --header 'Content-Type: application/json' \ --header 'Cookie: __Secure-better-auth.session_token=SPfVfKsV32HMCyP4sh1mhevInbAmQKvd.v4iXf9Lu0lcSNpDfz%2BHCfWKHEweqC0vEueyklbhA5Vk%3D' \ --data-raw '{ "name": "Prisma", "email": "prisma6@pixcel.org", "password": "password", "image": "", "gender": "MALE", "phone": "+60179611822" }' its not saving into db
Better Auth
Better Auth3mo ago
Hey there! Based on your configuration, here are a couple of things to check: 1. Your additionalFields configuration defines the role as a string array (type: "string[]") with input set to false. This means the client isn’t allowed to set it, and the default value is supposed to be used automatically. Ensure that your database (and its Prisma schema) has a column for role that accepts an array of strings. A mismatch in types can prevent the field from being inserted [12]. 2. In your database hook for user creation, you’re explicitly overriding the phone field with the value "not inserted." This will always replace any submitted phone value. Double-check if that’s the intended behavior for your application. 3. Also verify that your migrations are up-to-date and that the Prisma schema accurately reflects the additionalFields (role, gender, phone) you’ve defined in Better Auth. If the underlying database columns are missing or have incompatible types, the insertion might silently fail or be ignored. By confirming that the database schema matches your Better Auth configuration and that the before hook returns all required fields (including default values for fields set as input: false), you should be able to resolve why the data isn’t getting saved. Let me know if you have any other questions! Sources: [12] additionalFields does not save and is not show it, using authClient.signUp.email({
GitHub
better-auth/better-auth
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
GitHub
additionalFields does not save and is not show it, using authClient...
Is this suited for github? Yes, this is suited for github To Reproduce Ex git clone https://github.com/cesarve77/better-auth cd better-auth pnpm i pnpm run dev click on sign in, fill the form and c...

Did you find this page helpful?