Update JWT
This commit is contained in:
@@ -1,27 +1,21 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"BE-MiniERP/config"
|
||||
"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)),
|
||||
},
|
||||
claims := jwt.MapClaims{
|
||||
"user_id": userID,
|
||||
"role": role,
|
||||
"exp": time.Now().Add(time.Hour * 1).Unix(), // expired dalam 1 jam
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
return token.SignedString(jwtKey)
|
||||
|
||||
// INI PALING PENTING ⬇️
|
||||
return token.SignedString([]byte(config.GetConfig().JWTSecret))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user