diff options
| author | Bobby <[email protected]> | 2025-07-13 14:22:20 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2025-07-13 14:22:20 +0530 |
| commit | 3d7f8602d45583f25e2428bf6f8123453646dc08 (patch) | |
| tree | ecd707d298099ae9fda55efc3f0d1daf48f7b6e9 /models | |
| parent | bf112649d039f8f02e2135a74d8b506f7c31c784 (diff) | |
| download | imageboard-3d7f8602d45583f25e2428bf6f8123453646dc08.tar.xz imageboard-3d7f8602d45583f25e2428bf6f8123453646dc08.zip | |
registration controllers and email sending support
Diffstat (limited to 'models')
| -rw-r--r-- | models/tokens.go | 34 | ||||
| -rw-r--r-- | models/user.go | 19 |
2 files changed, 49 insertions, 4 deletions
diff --git a/models/tokens.go b/models/tokens.go new file mode 100644 index 0000000..c53ea2e --- /dev/null +++ b/models/tokens.go @@ -0,0 +1,34 @@ +package models + +import ( + "time" + + "gorm.io/gorm" +) + +type EmailToken struct { + gorm.Model + UserID uint `gorm:"not null;index" json:"user_id"` + Token string `gorm:"uniqueIndex;not null;size:64" json:"token"` + Type EmailTokenType `gorm:"not null;size:20" json:"type"` + ExpiresAt time.Time `gorm:"not null" json:"expires_at"` + UsedAt *time.Time `gorm:"default:null" json:"used_at"` + User User `gorm:"foreignKey:UserID" json:"user,omitempty"` +} + +func (et *EmailToken) IsExpired() bool { + return time.Now().After(et.ExpiresAt) +} + +func (et *EmailToken) IsUsed() bool { + return et.UsedAt != nil +} + +func (et *EmailToken) IsValid() bool { + return !et.IsExpired() && !et.IsUsed() +} + +func (et *EmailToken) MarkAsUsed() { + now := time.Now() + et.UsedAt = &now +} diff --git a/models/user.go b/models/user.go index 5607e68..3b92077 100644 --- a/models/user.go +++ b/models/user.go @@ -72,13 +72,27 @@ func (u *User) BeforeCreate(tx *gorm.DB) error { var userCount int64 if err := tx.Model(&User{}).Where("is_deleted = ?", false).Count(&userCount).Error; err != nil { - return err + return fmt.Errorf("failed to count existing users: %v", err) } if userCount == 0 { u.Level = UserLevelSuperAdmin // First user becomes Super Admin } + if len(u.Password) < config.Server.MinPasswordLength { + return fmt.Errorf("password must be at least %d characters long", config.Server.MinPasswordLength) + } + if len(u.Password) > 255 { + return fmt.Errorf("password must not exceed 255 characters") + } + + hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost) + if err != nil { + return err + } + + u.Password = string(hashedPassword) + return nil } @@ -93,16 +107,13 @@ func (u *User) SetPassword(password string) error { if len(password) < config.Server.MinPasswordLength { return fmt.Errorf("password must be at least %d characters long", config.Server.MinPasswordLength) } - if len(password) > 255 { return fmt.Errorf("password must not exceed 255 characters") } - hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) if err != nil { return err } - u.Password = string(hashedPassword) return nil } |
