Encriva for Developers

Better-Auth Enchanced/Plugins

Username

This plugin provides advanced username validation and management for Better Auth. It allows users to sign in with a username and password and adds strong validation rules.

Installation

import { username, usernameClient } from "@encriva/better-auth-enchanced";

To use this plugin in your application, you need to register it in both client and server configurations.

Initialize plugin

auth.ts
export const auth = betterAuth({
  plugins: [
    username()
  ]
});
auth-client.ts
const authClient = createAuthClient({
  plugins: [
    usernameClient()
  ]
});

Usage

To sign in with username:

authClient.signIn.username({
  username: "clqu",
  password: "1234567890"
});

To update username:

// To update username
authClient.updateUser({
  username: (document.getElementById("username") as HTMLInputElement).value
});

API

PropTypeDefault
schema?
Record<string, any>
-
transform?
(username: string) => string
(username) => username
validator?
(username: string) => boolean | Promise<boolean>
/^[a-zA-Z0-9_.]+$/
maxUsernameLength?
number
30
minUsernameLength?
number
3

Error Codes

CodeDescription
INVALID_USERNAME_OR_PASSWORDInvalid username or password
USERNAME_TOO_SHORTUsername is too short
USERNAME_TOO_LONGUsername is too long
INVALID_USERNAMEInvalid username format
USERNAME_IS_ALREADY_TAKENUsername is already taken
EMAIL_NOT_VERIFIEDEmail address is not verified

On this page