40 lines
853 B
Go
40 lines
853 B
Go
package utils
|
|
|
|
import (
|
|
"time"
|
|
|
|
timeutils "legalgo-BE-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)
|
|
|
|
// func GenerateToken(options ...ClaimOption) (string, error) {
|
|
// now := timeutils.Now()
|
|
|
|
// claims := jwt.MapClaims{
|
|
// string(jwtclaimenum.ISSUED_AT): now.Unix(),
|
|
// }
|
|
|
|
// for _, o := range options {
|
|
// o(claims)
|
|
// }
|
|
|
|
// token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
|
|
// return token.SignedString(jwtSecret)
|
|
// }
|
|
|
|
func GenerateToken(email string) (string, error) {
|
|
now := timeutils.Now()
|
|
token := jwt.New(jwt.SigningMethodHS256)
|
|
claims := token.Claims.(jwt.MapClaims)
|
|
claims["email"] = email
|
|
claims["exp"] = now.Add(time.Hour).Unix()
|
|
|
|
return token.SignedString(jwtSecret)
|
|
}
|