refac: register only open while no verified acc exists, enforced by a use case rather than handler & sends verification email whose link uses the conf pub URL whose code expires after 2h

This commit is contained in:
fsylvain committed 2026-09-24 03:36:37 +02:00
1 parent 6e9e108033
commit 6d510d2805
14 files changed
+575 -30

No files matched your search

+7 -16
View File
@@ -3,8 +3,10 @@ package auth
import (
"RenewCMS/api/pkg/apperr"
validator "RenewCMS/api/pkg/validator"
"RenewCMS/internal/domain/user"
"RenewCMS/internal/infrastructure/useCases"
"encoding/json"
"errors"
"net/http"
"time"
@@ -70,16 +72,6 @@ func (h *Handler) SetJwtCookie(w *http.ResponseWriter, userId uint32) error {
return nil
}
func (h *Handler) SomeUsersVerified() bool {
users := h.ListUseCase.ListUsers()
for _, localUser := range users {
if localUser.IsVerified {
return true
}
}
return false
}
func (h *Handler) IsUserTableEmpty() bool {
users := h.ListUseCase.ListUsers()
return len(users) == 0
@@ -138,11 +130,6 @@ func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
}
func (h *Handler) Register(w http.ResponseWriter, r *http.Request) {
if h.SomeUsersVerified() {
http.Error(w, "You are not allowed to create a user. Log in or reset database.", http.StatusForbidden)
return
}
var credentials RegisterCredentials
err := json.NewDecoder(r.Body).Decode(&credentials)
if err != nil {
@@ -162,7 +149,11 @@ func (h *Handler) Register(w http.ResponseWriter, r *http.Request) {
Email: credentials.Email,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
if errors.Is(err, user.ErrRegistrationClosed) {
http.Error(w, apperr.RegistrationClosedErrorMessage, http.StatusForbidden)
return
}
http.Error(w, apperr.RegistrationFailedErrorMessage, http.StatusInternalServerError)
return
}