deleted: main_dc/yalarba/api_es/internal/handler/auth_handler.go

modified:   main_dc/yalarba/api_es/internal/handler/user_handler.go
	modified:   main_dc/yalarba/api_es/internal/middleware/auth.go
	modified:   main_dc/yalarba/api_es/internal/service/user_service.go
fix bag with secret key
This commit is contained in:
2025-11-13 04:21:54 +05:00
parent 092ba74691
commit bdcf6ad431
4 changed files with 31 additions and 3 deletions
@@ -1,10 +1,13 @@
package middleware
import (
"api_es/internal/utils"
"api_es/pkg/logger"
"context"
"net/http"
"strings"
"api_es/internal/utils"
"go.uber.org/zap"
)
type contextKey string
@@ -16,21 +19,25 @@ const (
)
func AuthMiddleware(next http.Handler) http.Handler {
zapLogger := logger.Get()
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
zapLogger.Debug("Debug start AuthMiddleware")
authHeader := r.Header.Get("Authorization")
zapLogger.Debug("authHeader", zap.String("authHeader", authHeader))
if authHeader == "" {
http.Error(w, "Authorization header required", http.StatusUnauthorized)
return
}
tokenString := strings.Replace(authHeader, "Bearer ", "", 1)
zapLogger.Debug("tokenString", zap.String("tokenString", tokenString))
if tokenString == "" {
http.Error(w, "Invalid token", http.StatusUnauthorized)
return
}
// Здесь нужно использовать ваш JWT утилити
jwtUtil := utils.NewJWTUtil("your-secret-key")
jwtUtil := utils.NewJWTUtil("secret")
claims, err := jwtUtil.ValidateToken(tokenString)
if err != nil {
http.Error(w, "Invalid token", http.StatusUnauthorized)
@@ -40,6 +47,8 @@ func AuthMiddleware(next http.Handler) http.Handler {
ctx := context.WithValue(r.Context(), UserIDKey, claims.UserID)
ctx = context.WithValue(ctx, UserEmailKey, claims.Email)
ctx = context.WithValue(ctx, UserRoleKey, claims.Role)
zapLogger.Debug("Debug end AuthMiddleware")
next.ServeHTTP(w, r.WithContext(ctx))
})