36 lines
1.1 KiB
Go
36 lines
1.1 KiB
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// RefreshToken représente un token de rafraîchissement JWT
|
|
// MIGRATION UUID: UserID migré vers UUID
|
|
type RefreshToken struct {
|
|
ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"`
|
|
UserID uuid.UUID `gorm:"type:uuid;not null;index:idx_refresh_tokens_user_id" json:"user_id"`
|
|
TokenHash string `gorm:"not null;size:255;index:idx_refresh_tokens_token_hash" json:"-"`
|
|
ExpiresAt time.Time `gorm:"not null" json:"expires_at"`
|
|
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
|
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
|
|
|
// Relations
|
|
User User `gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE" json:"-"`
|
|
}
|
|
|
|
// TableName définit le nom de la table pour GORM
|
|
func (RefreshToken) TableName() string {
|
|
return "refresh_tokens"
|
|
}
|
|
|
|
// BeforeCreate hook GORM pour générer UUID si non défini
|
|
func (m *RefreshToken) BeforeCreate(tx *gorm.DB) error {
|
|
if m.ID == uuid.Nil {
|
|
m.ID = uuid.New()
|
|
}
|
|
return nil
|
|
}
|