31 lines
564 B
Go
31 lines
564 B
Go
package auth
|
|
|
|
import (
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
)
|
|
|
|
var secretKey = os.Getenv("JWT_SECRET_KEY")
|
|
|
|
type UserClaims struct {
|
|
jwt.RegisteredClaims
|
|
UserID int64
|
|
}
|
|
|
|
func GenerateUserToken(userId int64, expiryTime time.Time) (string, error) {
|
|
now := time.Now()
|
|
claims := UserClaims{
|
|
UserID: userId,
|
|
RegisteredClaims: jwt.RegisteredClaims{
|
|
IssuedAt: jwt.NewNumericDate(now),
|
|
ExpiresAt: jwt.NewNumericDate(expiryTime),
|
|
},
|
|
}
|
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
|
|
return token.SignedString([]byte(secretKey))
|
|
}
|