Docs

Username

The username plugin wraps the email and password authenticator and adds username support. This allows users to sign in and sign up with their username instead of their email.

Installation

Add Plugin to the server

auth.ts
import {  } from "better-auth"
import {  } from "better-auth/plugins"
 
const  = await ({
    : {
        : "sqlite",
        : "./db.sqlite",
    },
    : [ 
        () 
    ] 
})

Migrate the database

Run the migration to add the required fields to the user table.

This will add the following fields to the user table:

  • username: The username of the user.
npx better-auth migrate

Add the client plugin

client.ts
import {  } from "better-auth/client"
import {  } from "better-auth/client/plugins"
 
const  = ({
    : [ 
        () 
    ] 
})

Usage

Signup with username

To signup a user with username, you can use the signUp.username function provided by the client. The signUp function takes an object with the following properties:

  • username: The username of the user.
  • email: The email address of the user.
  • password: The password of the user. It should be at least 8 characters long and max 32 by default.
  • name: The name of the user.
  • image: The image of the user. (optional)
  • callbackURL: The url to redirect to after the user has signed up. (optional)
client.ts
 
const  = await ..({
    : "test",
    : "[email protected]",
    : "password1234",
    : "test",
    : "https://example.com/image.png",
})

Signin with username

To signin a user with username, you can use the signIn.username function provided by the client. The signIn function takes an object with the following properties:

  • username: The username of the user.
  • password: The password of the user.
  • callbackURL: The url to redirect to after the user has signed in. (optional)
client.ts
 
const  = await ..({
    : "test",
    : "password1234",
})

Schema

The plugin requires 1 field to be added to the user table:

Field NameTypeKeyDescription
username
string
-The username of the user

Options

The username plugin doesn't require any configuration. It just needs to be added to the server and client.

On this page

Edit on GitHub