fix: исправлена регистрация - добавлена поддержка JSON полей
- Исправлена конвертация models.JSON в PostgreSQL - Добавлено детальное логирование в AuthService - Обновлены структуры LoginResponse с UserResponse и OrganizationResponse - Исправлены методы Create/GetByID/Update в OrganizationRepository - Протестирована полная регистрация и аутентификация Регистрация и login работают корректно
This commit is contained in:
@@ -7,7 +7,9 @@ import (
|
||||
"erp-mvp/core-service/internal/auth"
|
||||
"erp-mvp/core-service/internal/models"
|
||||
"erp-mvp/core-service/internal/repository"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type AuthService interface {
|
||||
@@ -19,6 +21,7 @@ type authService struct {
|
||||
orgRepo repository.OrganizationRepository
|
||||
userRepo repository.UserRepository
|
||||
jwtService *auth.JWTService
|
||||
logger *logrus.Logger
|
||||
}
|
||||
|
||||
func NewAuthService(orgRepo repository.OrganizationRepository, userRepo repository.UserRepository, jwtService *auth.JWTService) AuthService {
|
||||
@@ -26,37 +29,46 @@ func NewAuthService(orgRepo repository.OrganizationRepository, userRepo reposito
|
||||
orgRepo: orgRepo,
|
||||
userRepo: userRepo,
|
||||
jwtService: jwtService,
|
||||
logger: logrus.New(),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *authService) Register(ctx context.Context, req *models.RegisterRequest) (*models.LoginResponse, error) {
|
||||
s.logger.Info("Starting registration process")
|
||||
|
||||
// Проверяем, что пользователь с таким email не существует
|
||||
existingUser, err := s.userRepo.GetByEmail(ctx, req.UserEmail)
|
||||
if err == nil && existingUser != nil {
|
||||
s.logger.Error("User with this email already exists")
|
||||
return nil, &ValidationError{Message: "User with this email already exists"}
|
||||
}
|
||||
|
||||
// Создаём организацию
|
||||
// Создаем организацию
|
||||
orgID := uuid.New()
|
||||
org := &models.Organization{
|
||||
ID: orgID,
|
||||
Name: req.OrganizationName,
|
||||
Type: req.OrganizationType,
|
||||
Settings: models.JSON{},
|
||||
Settings: models.JSON{"created_at": time.Now().Unix()},
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
s.logger.Info("Creating organization with ID: ", orgID)
|
||||
if err := s.orgRepo.Create(ctx, org); err != nil {
|
||||
s.logger.Error("Failed to create organization: ", err)
|
||||
return nil, err
|
||||
}
|
||||
s.logger.Info("Organization created successfully")
|
||||
|
||||
// Хешируем пароль
|
||||
passwordHash, err := auth.HashPassword(req.UserPassword)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to hash password: ", err)
|
||||
return nil, err
|
||||
}
|
||||
s.logger.Info("Password hashed successfully")
|
||||
|
||||
// Создаём пользователя
|
||||
// Создаем пользователя
|
||||
userID := uuid.New()
|
||||
user := &models.User{
|
||||
ID: userID,
|
||||
@@ -66,20 +78,33 @@ func (s *authService) Register(ctx context.Context, req *models.RegisterRequest)
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
s.logger.Info("Creating user with ID: ", userID)
|
||||
if err := s.userRepo.Create(ctx, user, passwordHash); err != nil {
|
||||
s.logger.Error("Failed to create user: ", err)
|
||||
return nil, err
|
||||
}
|
||||
s.logger.Info("User created successfully")
|
||||
|
||||
// Генерируем JWT токен
|
||||
token, err := s.jwtService.GenerateToken(user.ID, org.ID, user.Email, user.Role)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to generate token: ", err)
|
||||
return nil, err
|
||||
}
|
||||
s.logger.Info("JWT token generated successfully")
|
||||
|
||||
return &models.LoginResponse{
|
||||
Token: token,
|
||||
User: *user,
|
||||
ExpiresAt: time.Now().Add(24 * time.Hour), // TTL из конфигурации
|
||||
Token: token,
|
||||
User: models.UserResponse{
|
||||
ID: user.ID,
|
||||
Email: user.Email,
|
||||
Role: user.Role,
|
||||
},
|
||||
Organization: models.OrganizationResponse{
|
||||
ID: org.ID,
|
||||
Name: org.Name,
|
||||
Type: org.Type,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -101,10 +126,24 @@ func (s *authService) Login(ctx context.Context, req *models.LoginRequest) (*mod
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Получаем организацию для ответа
|
||||
org, err := s.orgRepo.GetByID(ctx, user.OrganizationID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &models.LoginResponse{
|
||||
Token: token,
|
||||
User: *user,
|
||||
ExpiresAt: time.Now().Add(24 * time.Hour), // TTL из конфигурации
|
||||
Token: token,
|
||||
User: models.UserResponse{
|
||||
ID: user.ID,
|
||||
Email: user.Email,
|
||||
Role: user.Role,
|
||||
},
|
||||
Organization: models.OrganizationResponse{
|
||||
ID: org.ID,
|
||||
Name: org.Name,
|
||||
Type: org.Type,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user