Base Project
This commit is contained in:
39
pkg/helper/jwt.go
Normal file
39
pkg/helper/jwt.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package helper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"wm-backend/global"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
// GenerateToken tạo JWT token cho user
|
||||
func GenerateToken(userID string) (string, error) {
|
||||
claims := jwt.MapClaims{
|
||||
"user_id": userID,
|
||||
"iat": time.Now().Unix(), // issued at
|
||||
"exp": time.Now().Add(time.Duration(global.Cfg.JWT.ExpirationHours) * time.Hour * 7).Unix(), // expiry
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
return token.SignedString([]byte(global.Cfg.JWT.SecretKey)) // <-- lấy từ config
|
||||
}
|
||||
|
||||
func ParseToken(tokenString string) (jwt.MapClaims, error) {
|
||||
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (any, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
return []byte(global.Cfg.JWT.SecretKey), nil // <-- lấy từ config
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
|
||||
return claims, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("invalid token")
|
||||
}
|
||||
22
pkg/helper/password.go
Normal file
22
pkg/helper/password.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package helper
|
||||
|
||||
import "golang.org/x/crypto/bcrypt"
|
||||
|
||||
// HashPassword generates a salted and hashed password using bcrypt.
|
||||
// It takes the plain-text password and the number of salt rounds as input.
|
||||
// It returns the generated salt, the hashed password, and any error encountered.
|
||||
func HashPassword(password string) (string, error) {
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(hashedPassword), nil
|
||||
}
|
||||
|
||||
// ComparePassword compares a plain-text password with a hashed password and returns true if they match.
|
||||
// It uses bcrypt.CompareHashAndPassword to perform the comparison.
|
||||
func ComparePassword(password string, hashedPassword string) error {
|
||||
err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
|
||||
return err
|
||||
}
|
||||
24
pkg/helper/validator.go
Normal file
24
pkg/helper/validator.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package helper
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"wm-backend/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
func IsShouldBindJSON(c *gin.Context, obj any) bool {
|
||||
if err := c.ShouldBindJSON(obj); err != nil {
|
||||
response.BadRequestError(c, http.StatusBadRequest, err.Error())
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
var validate = validator.New()
|
||||
|
||||
func IsEmail(input string) bool {
|
||||
err := validate.Var(input, "email")
|
||||
return err == nil
|
||||
}
|
||||
Reference in New Issue
Block a user