How do I make username a required field during sign up?

// auth.ts
export const auth = betterAuth({
appName: 'appname',
database: drizzleAdapter(db, {
provider: 'pg',
usePlural: true,
schema: {
users: UserTable,
sessions: SessionTable,
accounts: AccountTable,
verifications: VerificationTable,
},
}),
user: {
additionalFields: {
username: {
type: 'string',
required: true,
},
},
},
plugins: [
emailOTP({
async sendVerificationOTP({ email, otp, type }) {
// send email
},
sendVerificationOnSignUp: true,
otpLength: 6,
expiresIn: 10 * 60,
generateOTP(data, request) {
return '123456';
},
}),
username({
minUsernameLength: 3,
maxUsernameLength: 30,
usernameValidator: (username) => {
// Only allow alphanumeric characters, underscores, and dots
return /^[a-zA-Z0-9_.]+$/.test(username);
},
}),
openAPI(),
expo(),
],
emailAndPassword: {
enabled: true,
}
});
export const auth = betterAuth({
appName: 'appname',
database: drizzleAdapter(db, {
provider: 'pg',
usePlural: true,
schema: {
users: UserTable,
sessions: SessionTable,
accounts: AccountTable,
verifications: VerificationTable,
},
}),
user: {
additionalFields: {
username: {
type: 'string',
required: true,
},
},
},
plugins: [
emailOTP({
async sendVerificationOTP({ email, otp, type }) {
// send email
},
sendVerificationOnSignUp: true,
otpLength: 6,
expiresIn: 10 * 60,
generateOTP(data, request) {
return '123456';
},
}),
username({
minUsernameLength: 3,
maxUsernameLength: 30,
usernameValidator: (username) => {
// Only allow alphanumeric characters, underscores, and dots
return /^[a-zA-Z0-9_.]+$/.test(username);
},
}),
openAPI(),
expo(),
],
emailAndPassword: {
enabled: true,
}
});
// schema.ts
export const UserTable = pgTable('users', {
id: text('id').primaryKey(),
name: text('first_name').notNull(),
email: text('email').notNull().unique(),
emailVerified: boolean('email_verified').notNull(),
phone: varchar(),
image: text('image'),
role: userRole('role').default('user').notNull(),
banned: boolean('banned'),
banReason: text('ban_reason'),
banExpires: timestamp('ban_expires', { withTimezone: true }),
username: text('username').notNull().unique(),
displayUsername: text('display_username'),
...timestamps,
});
export const UserTable = pgTable('users', {
id: text('id').primaryKey(),
name: text('first_name').notNull(),
email: text('email').notNull().unique(),
emailVerified: boolean('email_verified').notNull(),
phone: varchar(),
image: text('image'),
role: userRole('role').default('user').notNull(),
banned: boolean('banned'),
banReason: text('ban_reason'),
banExpires: timestamp('ban_expires', { withTimezone: true }),
username: text('username').notNull().unique(),
displayUsername: text('display_username'),
...timestamps,
});
When I call the sign up with email endpoint with this payload,
{
"name": "Farhan Farooqui",
"email": "neetrasaudi3@gmail.com",
"password": "qwerty12345"
}
{
"name": "Farhan Farooqui",
"email": "neetrasaudi3@gmail.com",
"password": "qwerty12345"
}
I get error from DB, "insert into users failed..." instead of better auth's error
8 Replies
neetras
neetrasOP6d ago
user: {
additionalFields: {
username: {
type: 'string',
required: true,
},
lastName: {
type: 'string',
required: true,
},
},
},
user: {
additionalFields: {
username: {
type: 'string',
required: true,
},
lastName: {
type: 'string',
required: true,
},
},
},
When I add lastName as a required field, I do get errors if I don't send lastName
{
"code": "LASTNAME_IS_REQUIRED",
"message": "lastName is required"
}
{
"code": "LASTNAME_IS_REQUIRED",
"message": "lastName is required"
}
Why does it not work for username? 🙁
Ping
Ping6d ago
@Farhan you can pass schema configurations under the username plugin itself
neetras
neetrasOP6d ago
@Max can you please give me an example or point to the docs?
neetras
neetrasOP6d ago
not sure what to do here, there's no option to make it required
No description
Ping
Ping6d ago
oh interesting... I'll see what I can do to fix For now you should edit your DB schema to do this
Ping
Ping6d ago
If my PR lands then it will have built-in Better-Auth support for you to pass required: true for the fields https://github.com/better-auth/better-auth/pull/5330
GitHub
feat: support custom field attributes in plugin schemas by ping-max...
In the auth config, you can define a user/session/account/etc schema to customize the field attributes in a given table. Right now with plugin schemas, you can only customize the field name. With t...
Ping
Ping6d ago
@Farhan
neetras
neetrasOP6d ago
Wow that's a very quick PR! Thank you!

Did you find this page helpful?