Skip to content

plugins/username

A user sets a username (PATCH /username) and signs in with it, or with the email (POST /username/sign-in), through core’s password path: lockout, timing parity, the email-verification rule, the MFA gate and the login envelope are core’s. Usernames are unique without regard to case (the lowercased form sits under a unique index); the display form is kept as typed.

import "github.com/univeros/polaris.go/plugins/username"
cfg, err := polaris.FromEnv(db, username.New(username.Config{}))
p, err := polaris.New(cfg)
_, err = p.Migrate(ctx) // creates polaris_username

Every field is optional; the zero value is the default rules.

FieldDefaultPurpose
MinLength, MaxLength3, 30length bounds in characters
Patternusername.DefaultPattern ([A-Za-z0-9_.]+)what a username must match whole
Reservedusername.DefaultReserved (admin, root, me, …)replaces the default list when non-nil; an empty list reserves nothing
MethodPathPurpose
POST/username/sign-insign in with username (or the email) + password; core’s login envelope
PATCH/usernamebearer: set or change your username (display_username: same letters, another case)

Sign-in dispatches core’s user.logged_in, user.login_failed, user.locked. No events of its own; no permissions.

https://polaris.univeros.io/problems/username/invalid_credentials (401), …/username/email_unverified (403), …/username/account_disabled (403), …/username/invalid_input (422, missing username or password), …/username/invalid (422, breaks a rule; errors lists them), …/username/taken (409).

  • Erasing a user deletes its username inside the erasure’s transaction (wiring.UserEraser), so the name is free again.
  • username.Of(p.Graph()).Usernames() is the service: Set(ctx, userID, username, display), ForUser(ctx, userID), UserID(ctx, username), Violations(username). Set returns a *username.InvalidError (matches username.ErrInvalid) or username.ErrTaken.
  • username.Normalize is the stored (lowercased) form; case folding is strings.ToLower.
  • Erasing a user leaves its username row.
  • Rate-limit sign-in with plugins/sentinel: add username.SentinelRoutes to sentinel.Config.Routes (alongside sentinel.DefaultRoutes).

Example in example_test.go — go doc github.com/univeros/polaris.go/plugins/username.