aboutsummaryrefslogtreecommitdiff
path: root/models/tokens.go
blob: c635c4ed55aaf4a33912af41f250cec0e5bf1f37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package models

import (
	"imageboard/config"
	"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      config.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
}