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/tokens.go | |
| parent | bf112649d039f8f02e2135a74d8b506f7c31c784 (diff) | |
| download | imageboard-3d7f8602d45583f25e2428bf6f8123453646dc08.tar.xz imageboard-3d7f8602d45583f25e2428bf6f8123453646dc08.zip | |
registration controllers and email sending support
Diffstat (limited to 'models/tokens.go')
| -rw-r--r-- | models/tokens.go | 34 |
1 files changed, 34 insertions, 0 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 +} |
