Files
projet_gestion_commande/backend/gestion/utils/auth.go
T
2026-03-27 22:23:46 +01:00

61 lines
1.1 KiB
Go

package utils
import (
"crypto/rand"
"encoding/hex"
"regexp"
"strings"
"github.com/gin-gonic/gin"
)
func GenerateSessionID() string {
bytes := make([]byte, 16)
rand.Read(bytes)
return hex.EncodeToString(bytes)
}
func ValidatePhoneNumber(phone string) bool {
clean := regexp.MustCompile(`[\s\-\(\)]`).ReplaceAllString(phone, "")
validFormat := regexp.MustCompile(`^(\+33|0)[1-9]\d{8}$`)
return validFormat.MatchString(clean)
}
func NormalizePhoneNumber(phone string) string {
clean := regexp.MustCompile(`[\s\-\(\)]`).ReplaceAllString(phone, "")
if strings.HasPrefix(clean, "0") {
return "+33" + clean[1:]
}
return clean
}
func CheckRoleAdmin(c *gin.Context, role string) bool {
if role == "admin" {
return true
}
return false
}
func CheckRoleClient(c *gin.Context, role string) bool {
if role == "client" {
return true
}
return false
}
func CheckRoleCabine(c *gin.Context, role string) bool {
if role == "cabine" {
return true
}
return false
}
func CheckRoleLivreur(c *gin.Context, role string) bool {
if role == "livreur" {
return true
}
return false
}