32 lines
743 B
Go
32 lines
743 B
Go
package service
|
|
|
|
import (
|
|
"BE-MiniERP/config"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt/v4"
|
|
)
|
|
|
|
func GenerateJWT(userID uint, role string) (string, error) {
|
|
claims := jwt.MapClaims{
|
|
"user_id": userID,
|
|
"role": role,
|
|
"exp": time.Now().Add(time.Hour * 24).Unix(), // expired dalam 1 jam
|
|
}
|
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
|
|
// INI PALING PENTING ⬇️
|
|
return token.SignedString([]byte(config.GetConfig().JWTSecret))
|
|
}
|
|
|
|
func GenerateRefreshToken(userID uint) (string, error) {
|
|
claims := jwt.MapClaims{
|
|
"user_id": userID,
|
|
"exp": time.Now().Add(time.Hour * 24).Unix(),
|
|
}
|
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
return token.SignedString([]byte(config.GetConfig().JWTSecret))
|
|
}
|