28 lines
563 B
Go
28 lines
563 B
Go
package service
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt/v4"
|
|
)
|
|
|
|
var jwtKey = []byte("supersecret") // sebaiknya dari env
|
|
|
|
type Claims struct {
|
|
UserID uint `json:"user_id"`
|
|
Role string `json:"role"`
|
|
jwt.RegisteredClaims
|
|
}
|
|
|
|
func GenerateJWT(userID uint, role string) (string, error) {
|
|
claims := &Claims{
|
|
UserID: userID,
|
|
Role: role,
|
|
RegisteredClaims: jwt.RegisteredClaims{
|
|
ExpiresAt: jwt.NewNumericDate(time.Now().Add(24 * time.Hour)),
|
|
},
|
|
}
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
return token.SignedString(jwtKey)
|
|
}
|