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:
@@ -1 +0,0 @@
|
||||
package handler
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"api_es/internal/dto"
|
||||
appMiddleware "api_es/internal/middleware"
|
||||
"api_es/internal/service"
|
||||
"api_es/pkg/logger"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-playground/validator/v10"
|
||||
@@ -36,6 +37,8 @@ func NewUserHandler(userService service.UserService) *UserHandler {
|
||||
// @Failure 500 {object} map[string]string
|
||||
// @Router /auth/register [post]
|
||||
func (h *UserHandler) Register(w http.ResponseWriter, r *http.Request) {
|
||||
zapLogger := logger.Get()
|
||||
zapLogger.Debug("Start register")
|
||||
var req dto.RegisterRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||
@@ -60,6 +63,7 @@ func (h *UserHandler) Register(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
zapLogger.Debug("End register")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
@@ -75,6 +79,8 @@ func (h *UserHandler) Register(w http.ResponseWriter, r *http.Request) {
|
||||
// @Failure 401 {object} map[string]string
|
||||
// @Router /auth/login [post]
|
||||
func (h *UserHandler) Login(w http.ResponseWriter, r *http.Request) {
|
||||
zapLogger := logger.Get()
|
||||
zapLogger.Debug("Start login")
|
||||
var req dto.LoginRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||
@@ -98,6 +104,7 @@ func (h *UserHandler) Login(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
zapLogger.Debug("End login")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
@@ -112,6 +119,8 @@ func (h *UserHandler) Login(w http.ResponseWriter, r *http.Request) {
|
||||
// @Failure 404 {object} map[string]string
|
||||
// @Router /users/profile [get]
|
||||
func (h *UserHandler) GetProfile(w http.ResponseWriter, r *http.Request) {
|
||||
zapLogger := logger.Get()
|
||||
zapLogger.Debug("GetProfile start debug level")
|
||||
userID, ok := r.Context().Value(appMiddleware.UserIDKey).(uint)
|
||||
if !ok {
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
@@ -125,6 +134,7 @@ func (h *UserHandler) GetProfile(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
zapLogger.Debug("GetProfile end debug level")
|
||||
json.NewEncoder(w).Encode(user)
|
||||
}
|
||||
|
||||
@@ -203,6 +213,8 @@ func (h *UserHandler) GetUser(w http.ResponseWriter, r *http.Request) {
|
||||
// @Success 200 {array} dto.UserResponse
|
||||
// @Router /users [get]
|
||||
func (h *UserHandler) ListUsers(w http.ResponseWriter, r *http.Request) {
|
||||
zapLogger := logger.Get()
|
||||
zapLogger.Debug("Debug start handler listUsers")
|
||||
limitStr := r.URL.Query().Get("limit")
|
||||
offsetStr := r.URL.Query().Get("offset")
|
||||
|
||||
@@ -228,6 +240,7 @@ func (h *UserHandler) ListUsers(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
zapLogger.Debug("Debug end handler listUsers")
|
||||
json.NewEncoder(w).Encode(users)
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
@@ -41,6 +48,8 @@ func AuthMiddleware(next http.Handler) http.Handler {
|
||||
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))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"api_es/internal/models"
|
||||
"api_es/internal/repository"
|
||||
"api_es/internal/utils"
|
||||
"api_es/pkg/logger"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
@@ -41,6 +42,8 @@ func NewUserService(userRepo repository.UserRepository, jwtUtil *utils.JWTUtil)
|
||||
}
|
||||
|
||||
func (s *userService) Register(ctx context.Context, req dto.RegisterRequest) (*dto.AuthResponse, error) {
|
||||
zapLogger := logger.Get()
|
||||
zapLogger.Debug("Start register")
|
||||
// Проверяем существование пользователя
|
||||
existingUser, _ := s.userRepo.GetByEmail(ctx, req.Email)
|
||||
if existingUser != nil {
|
||||
@@ -78,6 +81,7 @@ func (s *userService) Register(ctx context.Context, req dto.RegisterRequest) (*d
|
||||
}
|
||||
|
||||
userResponse := dto.ToUserResponse(user)
|
||||
zapLogger.Debug("End register")
|
||||
return &dto.AuthResponse{
|
||||
Token: token,
|
||||
User: userResponse,
|
||||
@@ -85,6 +89,8 @@ func (s *userService) Register(ctx context.Context, req dto.RegisterRequest) (*d
|
||||
}
|
||||
|
||||
func (s *userService) Login(ctx context.Context, req dto.LoginRequest) (*dto.AuthResponse, error) {
|
||||
zapLogger := logger.Get()
|
||||
zapLogger.Debug("Start login")
|
||||
// Находим пользователя по email
|
||||
user, err := s.userRepo.GetByEmail(ctx, req.Email)
|
||||
if err != nil {
|
||||
@@ -108,6 +114,7 @@ func (s *userService) Login(ctx context.Context, req dto.LoginRequest) (*dto.Aut
|
||||
}
|
||||
|
||||
userResponse := dto.ToUserResponse(user)
|
||||
zapLogger.Debug("End login")
|
||||
return &dto.AuthResponse{
|
||||
Token: token,
|
||||
User: userResponse,
|
||||
|
||||
Reference in New Issue
Block a user