summaryrefslogtreecommitdiff
path: root/shrine/models/token.go
blob: ed03dcc85872c14e2b30607cb712f854952e28f0 (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
package models

import (
	"time"

	"gorm.io/gorm"
)

type Token struct {
	gorm.Model
	TokenHash string `gorm:"uniqueIndex;size:64;not null"`
	UserID    uint   `gorm:"index;not null"`
	User      User   `gorm:"foreignKey:UserID"`
	ExpiresAt time.Time
	IPAddress string `gorm:"size:45"`
	UserAgent string `gorm:"size:512"`
}

func (self *Token) BeforeCreate(tx *gorm.DB) error {
	if self.TokenHash == "" {
		return gorm.ErrInvalidData
	}
	if self.UserID == 0 {
		return gorm.ErrInvalidData
	}
	if self.ExpiresAt.IsZero() {
		return gorm.ErrInvalidData
	}
	return nil
}