summaryrefslogtreecommitdiff
path: root/nexus/models/account.go
diff options
context:
space:
mode:
authorBobby <[email protected]>2026-03-29 22:52:46 +0530
committerBobby <[email protected]>2026-03-29 22:52:46 +0530
commit9eb9b7f4bd552a641235764f66483e1f940fcfd9 (patch)
treeda520b923b5e6758d5457b6233dd6671fc640914 /nexus/models/account.go
parent65a143a0871c35989b7c7ea6723d39a0585c089e (diff)
downloadechoes-of-vaelun-main.tar.xz
echoes-of-vaelun-main.zip
feat: nexus account manager scaffold with auth, characters, realmsHEADmain
Diffstat (limited to 'nexus/models/account.go')
-rw-r--r--nexus/models/account.go77
1 files changed, 77 insertions, 0 deletions
diff --git a/nexus/models/account.go b/nexus/models/account.go
new file mode 100644
index 0000000..d6e8b5a
--- /dev/null
+++ b/nexus/models/account.go
@@ -0,0 +1,77 @@
+package models
+
+import (
+ "errors"
+ "nexus/types/account"
+ "nexus/utils/validate"
+ "strings"
+
+ "github.com/google/uuid"
+ "golang.org/x/crypto/bcrypt"
+ "gorm.io/gorm"
+)
+
+type Account struct {
+ gorm.Model
+ ID uuid.UUID `gorm:"type:uuid;primaryKey"`
+ Username string `gorm:"uniqueIndex;not null"`
+ Email string `gorm:"uniqueIndex;not null"`
+ PasswordHash string `gorm:"not null"`
+ IsActive bool `gorm:"default:true"`
+ IsVerified bool `gorm:"default:false"`
+ Characters []Character `gorm:"foreignKey:AccountID"`
+ Sessions []Session `gorm:"foreignKey:AccountID"`
+}
+
+func (self *Account) BeforeCreate(tx *gorm.DB) error {
+ if self.ID == uuid.Nil {
+ self.ID = uuid.New()
+ }
+
+ self.Email = strings.TrimSpace(strings.ToLower(self.Email))
+ self.Username = strings.TrimSpace(self.Username)
+
+ if !validate.Email(self.Email) {
+ return errors.New(validate.InvalidEmail)
+ }
+
+ if self.Username == "" {
+ return errors.New("username is required")
+ }
+
+ return nil
+}
+
+func (self *Account) BeforeUpdate(tx *gorm.DB) error {
+ self.Email = strings.TrimSpace(strings.ToLower(self.Email))
+ self.Username = strings.TrimSpace(self.Username)
+
+ if !validate.Email(self.Email) {
+ return errors.New(validate.InvalidEmail)
+ }
+
+ return nil
+}
+
+func (self *Account) SetPassword(password string) error {
+ hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
+ if err != nil {
+ return err
+ }
+ self.PasswordHash = string(hash)
+ return nil
+}
+
+func (self *Account) CheckPassword(password string) bool {
+ return bcrypt.CompareHashAndPassword([]byte(self.PasswordHash), []byte(password)) == nil
+}
+
+func (self *Account) ToResponse() account.Response {
+ return account.Response{
+ ID: self.ID,
+ Username: self.Username,
+ Email: self.Email,
+ IsVerified: self.IsVerified,
+ CreatedAt: self.CreatedAt,
+ }
+}