password field in auth collections. It automatically hashes plain text passwords and provides validation methods.
Configuration options
Optional regex pattern to match against the plain password value. Leave empty to skip pattern validation.
Minimum required password length (in characters). Set to 0 for no minimum.
Maximum allowed password length (in characters). Defaults to 71 (bcrypt limit) if zero or not set.
Bcrypt cost factor (4-31). Higher values increase security but take longer to hash. Defaults to bcrypt.DefaultCost (10) if zero.
When true, requires the field value to be a non-empty string.
How it works
The password field has special behavior:- Setting values: When you set a plain text password using
record.Set(), it’s automatically hashed - Getting values:
record.Get()returns the plain password only before the record is saved, then returns empty string - Hash access: Use
record.GetString("password:hash")to access the bcrypt hash - Direct hash: Use
record.SetRaw()to set a pre-hashed bcrypt string directly
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
minandmaxcharacters - 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
- Basic usage
- With pattern validation
- Custom bcrypt cost
- Setting pre-hashed password
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"".