hiling_go/util/token/payload.go
2024-05-22 11:26:15 +07:00

37 lines
708 B
Go
Executable File

package token
import (
"errors"
"time"
)
var (
ErrExpiredToken = errors.New("token has expired")
ErrInvalidToken = errors.New("token is invalid")
)
type Payload struct {
Username string `json:"email"`
UserID int `json:"user_id"`
IssuedAt time.Time `json:"issued_at"`
ExpiredAt time.Time `json:"expired_at"`
}
func NewPayload(username string, user_id int, duration time.Duration) (*Payload, error) {
payload := &Payload{
Username: username,
UserID: user_id,
IssuedAt: time.Now(),
ExpiredAt: time.Now().Add(duration),
}
return payload, nil
}
func (payload *Payload) Valid() error {
if time.Now().After(payload.ExpiredAt) {
return ErrExpiredToken
}
return nil
}