Skip to main content
The password field stores bcrypt-hashed passwords and is primarily used for the system password field in auth collections. It automatically hashes plain text passwords and provides validation methods.

Configuration options

string
Optional regex pattern to match against the plain password value. Leave empty to skip pattern validation.
int
default:"0"
Minimum required password length (in characters). Set to 0 for no minimum.
int
default:"71"
Maximum allowed password length (in characters). Defaults to 71 (bcrypt limit) if zero or not set.
int
default:"bcrypt.DefaultCost"
Bcrypt cost factor (4-31). Higher values increase security but take longer to hash. Defaults to bcrypt.DefaultCost (10) if zero.
bool
default:"false"
When true, requires the field value to be a non-empty string.

How it works

The password field has special behavior:
  1. Setting values: When you set a plain text password using record.Set(), it’s automatically hashed
  2. Getting values: record.Get() returns the plain password only before the record is saved, then returns empty string
  3. Hash access: Use record.GetString("password:hash") to access the bcrypt hash
  4. Direct hash: Use record.SetRaw() to set a pre-hashed bcrypt string directly
Bcrypt has a maximum password length of 72 bytes. The field enforces a 71 character limit by default to account for encoding.

Special getter

The password field provides a special getter to access the hash:

Validation rules

The password field validates:
  • Length: Plain password must be between min and max characters
  • Pattern: If specified, plain password must match the regex pattern
  • Hash errors: Bcrypt hashing errors are captured and returned during validation
  • Required: If enabled, hash must be non-empty

Go examples

Password validation

The password field value can be validated against a plain text password:

Database column type

Common password patterns

Bcrypt cost levels

Bcrypt cost determines how many iterations are used. Higher cost = exponentially more time:
  • Cost 4: ~2ms (testing only)
  • Cost 10: ~50ms (default, good balance)
  • Cost 12: ~200ms (high security)
  • Cost 14: ~800ms (very high security)
  • Cost 15+: Use with caution (can take several seconds)

Security best practices

  • Never store or log plain text passwords
  • Use a minimum length of at least 8 characters (12+ recommended)
  • Consider requiring complexity through pattern validation
  • Use default bcrypt cost (10) unless you have specific security requirements
  • The plain password is automatically cleared after save
  • Bcrypt automatically includes a salt, no need to add one separately
  • Hashed passwords are approximately 60 characters long

Auth collection integration

This field is automatically used in auth collections:

Zero value

The zero value for password fields is an empty string "".