1
0
forked from baron/baron-sso

fix(backend): prevent duplicate key constraint on empty login id when syncing users

This commit is contained in:
2026-03-31 13:11:32 +09:00
parent 4b34ab8161
commit 5029b8049b
6 changed files with 154 additions and 18 deletions

View File

@@ -496,9 +496,51 @@ func (h *AuthHandler) Signup(c *fiber.Ctx) error {
return errorJSON(c, fiber.StatusForbidden, "The specified organization is not active.")
}
} else {
// If companyCode provided but not found, we should probably reject if we want strictness,
// or just treat as GENERAL user. Given the risk "존재하지 않는 테넌트도 저장됨", we should reject.
return errorJSON(c, fiber.StatusBadRequest, "해당하는 가족사(테넌트)를 찾을 수 없습니다.")
// If companyCode provided but not found, we automatically create one
// [New Policy] 자동 생성 로직 추가
slog.Info("[Signup] CompanyCode not found, creating new tenant automatically", "slug", req.CompanyCode)
// Determine name from CompanyCode
tenantName := req.CompanyCode
// Map slug to localized name if possible
slugToName := map[string]string{
"HANMAC": "한맥",
"SAMAN": "삼안",
"JANGHEON": "장헌",
"HALLA": "한라",
"PTC": "PTC",
"BARON": "바론",
}
if name, ok := slugToName[strings.ToUpper(req.CompanyCode)]; ok {
tenantName = name
}
// Create the tenant
// Note: creatorID is unknown at this point, will be set via Read-Model sync later
newTenant, err := h.TenantService.RegisterTenant(c.Context(),
tenantName,
req.CompanyCode,
domain.TenantTypeCompany,
"Automatically created during signup",
nil, // domains
nil, // parentID
"", // creatorID (will sync later)
)
if err != nil {
// Handle race condition: if tenant was created by another request just now
if strings.Contains(err.Error(), "already exists") {
newTenant, err = h.TenantService.GetTenantBySlug(c.Context(), req.CompanyCode)
}
if err != nil || newTenant == nil {
slog.Error("[Signup] Failed to create tenant automatically", "slug", req.CompanyCode, "error", err)
return errorJSON(c, fiber.StatusInternalServerError, "Failed to initialize organization.")
}
}
slog.Info("[Signup] Successfully created missing tenant", "slug", req.CompanyCode, "id", newTenant.ID)
tenantID = &newTenant.ID
companyCode = newTenant.Slug
}
}