Update API

This commit is contained in:
2025-06-22 23:23:26 +07:00
parent b077ef9f06
commit 40705fbf3b
63 changed files with 1976 additions and 81 deletions
+15
View File
@@ -0,0 +1,15 @@
package service
import (
"golang.org/x/crypto/bcrypt"
)
func HashPassword(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
return string(bytes), err
}
func CheckPasswordHash(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}
+27
View File
@@ -0,0 +1,27 @@
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)
}