// Package accounts holds functions that interact with the accounts package accounts import ( "errors" "regexp" "git.stinnesbeck.com/nils/artifacts/api" ) // Account is a struct that holds info on an account type Account struct { Username string `json:"username"` Password string `json:"password"` Email string `json:"email"` } // custom Error codes var ( ErrUsernameTooShort = errors.New("username is too short, it needs to be at least 6 chars long") ErrUsernameTooLong = errors.New("username is too long, it needs to be at max 32 chars long") ErrUsernameRegex = errors.New(`username must pass this regex: ^[a-zA-Z0-9_-]+$`) ErrPasswordTooShort = errors.New("password is too short, it needs to be at least 5 chars long") ErrPasswordTooLong = errors.New("password is too long, it needs to be at max 50 chars long") ErrPasswordRegex = errors.New(`password must pass this regex: ^[^\s]+$`) ) // Create can be used to create an account func Create(username, password, email string) error { // >= 6 characters<= 32 characters userRegex := regexp.MustCompile(`^[a-zA-Z0-9_-]+$`) passRegex := regexp.MustCompile(`^[^\s]+$`) // username checks switch { case len(username) < 6: return ErrUsernameTooShort case len(username) > 32: return ErrUsernameTooLong case !userRegex.MatchString(username): return ErrUsernameRegex } // password checks switch { case len(password) < 5: return ErrPasswordTooShort case len(password) > 50: return ErrPasswordTooLong case !passRegex.MatchString(password): return ErrPasswordRegex } // these are the possible response codes, other than 200 OK responseCodes := map[int]string{ 456: "Username already used.", 457: "Email already used.", } newAccount := Account{ Username: username, Password: password, Email: email, } _, err := api.Post[string]("/accounts/create", newAccount, responseCodes) if err != nil { return err } return nil }