summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby <[email protected]>2026-03-05 15:26:20 +0530
committerBobby <[email protected]>2026-03-05 15:26:20 +0530
commite7d1f4d01f37cfa30347ce3d9e93975abd474fcb (patch)
treefe1b585ecf381f9a5d25cba74eddd2f799e2a6e2
parent01e69afac6a486dacf5fff6c3239a514c3cfb1ab (diff)
downloadpagoda-e7d1f4d01f37cfa30347ce3d9e93975abd474fcb.tar.xz
pagoda-e7d1f4d01f37cfa30347ce3d9e93975abd474fcb.zip
feat: enhance user feedback with specific error messages for registration and validation
-rw-r--r--shrine/controllers/auth.go7
-rw-r--r--shrine/models/user.go20
2 files changed, 17 insertions, 10 deletions
diff --git a/shrine/controllers/auth.go b/shrine/controllers/auth.go
index 1e49b37..fc14d2a 100644
--- a/shrine/controllers/auth.go
+++ b/shrine/controllers/auth.go
@@ -3,6 +3,7 @@ package controllers
import (
"errors"
"shrine/enums"
+ "strings"
"shrine/models"
"shrine/repositories"
"shrine/types"
@@ -32,6 +33,12 @@ func RegisterController(context *fiber.Ctx) error {
}
if err := repositories.CreateUser(&user); err != nil {
+ if strings.Contains(err.Error(), "users.username") {
+ return BadRequest(context, errors.New("An account with that username already exists."))
+ }
+ if strings.Contains(err.Error(), "users.email") {
+ return BadRequest(context, errors.New("An account with that email address already exists."))
+ }
return BadRequest(context, err)
}
diff --git a/shrine/models/user.go b/shrine/models/user.go
index fa4a2f9..96a181f 100644
--- a/shrine/models/user.go
+++ b/shrine/models/user.go
@@ -1,7 +1,7 @@
package models
import (
- "fmt"
+ "errors"
"shrine/enums"
"shrine/types"
"shrine/utils/validators"
@@ -45,10 +45,10 @@ type User struct {
func (user *User) SetPassword(password string) error {
if len(password) < 8 {
- return fmt.Errorf("password must be at least 8 characters")
+ return errors.New("Password must be at least 8 characters.")
}
if len(password) > 255 {
- return fmt.Errorf("password must be at most 255 characters")
+ return errors.New("Password must be at most 255 characters.")
}
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
@@ -127,23 +127,23 @@ func (user *User) BeforeCreate(tx *gorm.DB) error {
if !bypassUsername {
if !validators.IsValidUsername(user.Username, 3) {
- return fmt.Errorf("username must be 3-32 characters, alphanumeric and underscores only")
+ return errors.New("Username must be 3-32 characters and can only contain letters, numbers, and underscores.")
}
if validators.IsReservedUsername(user.Username) {
- return fmt.Errorf("username is reserved")
+ return errors.New("This username is not available.")
}
}
if !validators.IsValidEmail(user.Email) {
- return fmt.Errorf("invalid email format")
+ return errors.New("Please enter a valid email address.")
}
if len(strings.TrimSpace(user.DisplayName)) < 1 || len(strings.TrimSpace(user.DisplayName)) > 50 {
- return fmt.Errorf("display name must be between 1 and 50 characters")
+ return errors.New("Display name must be between 1 and 50 characters.")
}
if user.PasswordHash == "" {
- return fmt.Errorf("password is required")
+ return errors.New("Password is required.")
}
user.Email = strings.ToLower(strings.TrimSpace(user.Email))
@@ -155,11 +155,11 @@ func (user *User) BeforeCreate(tx *gorm.DB) error {
func (user *User) BeforeUpdate(tx *gorm.DB) error {
if !validators.IsValidEmail(user.Email) {
- return fmt.Errorf("invalid email format")
+ return errors.New("Please enter a valid email address.")
}
if len(strings.TrimSpace(user.DisplayName)) < 1 || len(strings.TrimSpace(user.DisplayName)) > 50 {
- return fmt.Errorf("display name must be between 1 and 50 characters")
+ return errors.New("Display name must be between 1 and 50 characters.")
}
user.Email = strings.ToLower(strings.TrimSpace(user.Email))