37 lines
702 B
Go
37 lines
702 B
Go
package token
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
ErrExpiredToken = errors.New("token has expired")
|
|
ErrInvalidToken = errors.New("token is invalid")
|
|
)
|
|
|
|
type Payload struct {
|
|
Email string `json:"email"`
|
|
UserID int `json:"user_id"`
|
|
IssuedAt time.Time `json:"issued_at"`
|
|
ExpiredAt time.Time `json:"expired_at"`
|
|
}
|
|
|
|
func NewPayload(email string, user_id int, duration time.Duration) (*Payload, error) {
|
|
payload := &Payload{
|
|
Email: email,
|
|
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
|
|
}
|