mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-10 12:27:44 +00:00
feat(username): add default validation and options for validating username (#1345)
* feat: add default validation and options for validating username * chore: release v1.1.16-beta.5 * fix: include update-user * chore: release v1.1.16-beta.6
This commit is contained in:
committed by
Bereket Engida
parent
1affe01986
commit
c4f2087943
@@ -110,3 +110,60 @@ The plugin requires 1 field to be added to the user table:
|
||||
},
|
||||
]}
|
||||
/>
|
||||
|
||||
## Options
|
||||
|
||||
### Min Username Length
|
||||
|
||||
The minimum length of the username. Default is `3`.
|
||||
|
||||
```ts title="auth.ts"
|
||||
import { betterAuth } from "better-auth"
|
||||
import { username } from "better-auth/plugins"
|
||||
|
||||
const auth = betterAuth({
|
||||
plugins: [
|
||||
username({
|
||||
minUsernameLength: 5
|
||||
})
|
||||
]
|
||||
})
|
||||
```
|
||||
|
||||
### Max Username Length
|
||||
|
||||
The maximum length of the username. Default is `30`.
|
||||
|
||||
```ts title="auth.ts"
|
||||
import { betterAuth } from "better-auth"
|
||||
import { username } from "better-auth/plugins"
|
||||
|
||||
const auth = betterAuth({
|
||||
plugins: [
|
||||
username({
|
||||
maxUsernameLength: 100
|
||||
})
|
||||
]
|
||||
})
|
||||
```
|
||||
|
||||
### Username Validator
|
||||
|
||||
A function that validates the username. The function should return false if the username is invalid. By default, the username should only contain alphanumeric characters and underscores.
|
||||
|
||||
```ts title="auth.ts"
|
||||
import { betterAuth } from "better-auth"
|
||||
import { username } from "better-auth/plugins"
|
||||
|
||||
const auth = betterAuth({
|
||||
plugins: [
|
||||
username({
|
||||
usernameValidator: (username) => {
|
||||
if (username === "admin") {
|
||||
return false
|
||||
}
|
||||
}
|
||||
})
|
||||
]
|
||||
})
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user