39 lines
876 B
Go
Raw Normal View History

2025-02-22 12:50:26 +08:00
package utils
import (
2025-02-22 16:15:38 +08:00
"time"
2025-02-22 12:50:26 +08:00
timeutils "github.com/ardeman/project-legalgo-go/internal/utilities/time_utils"
"github.com/golang-jwt/jwt/v5"
)
var jwtSecret = []byte("secret jwt key") // TODO: change later from env
type ClaimOption func(options jwt.MapClaims)
2025-02-24 16:48:20 +08:00
// func GenerateToken(options ...ClaimOption) (string, error) {
// now := timeutils.Now()
2025-02-22 12:50:26 +08:00
2025-02-24 16:48:20 +08:00
// claims := jwt.MapClaims{
// string(jwtclaimenum.ISSUED_AT): now.Unix(),
// }
2025-02-22 12:50:26 +08:00
2025-02-24 16:48:20 +08:00
// for _, o := range options {
// o(claims)
// }
2025-02-22 12:50:26 +08:00
2025-02-24 16:48:20 +08:00
// token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
2025-02-22 12:50:26 +08:00
2025-02-24 16:48:20 +08:00
// return token.SignedString(jwtSecret)
// }
2025-02-22 16:15:38 +08:00
2025-02-24 16:48:20 +08:00
func GenerateToken(email string) (string, error) {
2025-02-22 16:15:38 +08:00
now := timeutils.Now()
token := jwt.New(jwt.SigningMethodHS256)
2025-02-22 16:15:38 +08:00
claims := token.Claims.(jwt.MapClaims)
claims["email"] = email
2025-02-22 16:15:38 +08:00
claims["exp"] = now.Add(time.Hour).Unix()
return token.SignedString(jwtSecret)
}