hiling_go/util/token/payload.go
2023-09-21 21:38:41 +07:00

37 lines
708 B
Go

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
}