sso/internal/lib/jwt/jwt.go

26 lines
533 B
Go
Raw Permalink Normal View History

2023-12-17 12:17:55 +02:00
package jwt
import (
"sso/internal/domain/models"
"time"
"github.com/golang-jwt/jwt/v5"
)
func NewToken(user models.User, app_id int, secret string, duration time.Duration) (string, error) {
token := jwt.New(jwt.SigningMethodHS256)
claims := token.Claims.(jwt.MapClaims)
claims["uid"] = user.ID
claims["email"] = user.Email
claims["exp"] = time.Now().Add(duration).Unix()
claims["app_id"] = app_id
tokenString, err := token.SignedString([]byte(secret))
if err != nil {
return "", err
}
return tokenString, nil
}