feat: HTTP panel on :8000 with first admin setup
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"regexp"
|
||||
"unicode/utf8"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrPasswordTooShort = errors.New("password must be at least 8 characters")
|
||||
ErrInvalidEmail = errors.New("invalid email")
|
||||
ErrInvalidUsername = errors.New("username must be 3-64 characters: letters, digits, underscore")
|
||||
)
|
||||
|
||||
var (
|
||||
emailRegex = regexp.MustCompile(`^[^\s@]+@[^\s@]+\.[^\s@]+$`)
|
||||
usernameRegex = regexp.MustCompile(`^[a-zA-Z0-9_]{3,64}$`)
|
||||
)
|
||||
|
||||
func HashPassword(password string) (string, error) {
|
||||
if utf8.RuneCountInString(password) < 8 {
|
||||
return "", ErrPasswordTooShort
|
||||
}
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(hash), nil
|
||||
}
|
||||
|
||||
func ValidateEmail(email string) error {
|
||||
if !emailRegex.MatchString(email) {
|
||||
return ErrInvalidEmail
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateUsername(username string) error {
|
||||
if !usernameRegex.MatchString(username) {
|
||||
return ErrInvalidUsername
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user