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

+1 -1
View File
@@ -80,7 +80,7 @@ func getUseCases(repos *Repositories, settings config.Settings) *UseCases {
UpdateArticleUseCase: useCases.NewUpdateArticleUseCase(repos.ArticleRepo),
DeleteArticleUseCase: useCases.NewDeleteArticleUseCase(repos.ArticleRepo, repos.ImageRepo),
GetUserUseCase: useCases.NewGetUserUseCase(repos.UserRepo),
CreateUserUseCase: useCases.NewCreateUserUseCase(repos.UserRepo, hasher),
CreateUserUseCase: useCases.NewCreateUserUseCase(repos.UserRepo, hasher, repos.MailRepo, settings.PublicURL),
UpdateUserUseCase: useCases.NewUpdateUserUseCase(repos.UserRepo, hasher),
AuthenticateUserUseCase: useCases.NewAuthenticateUserUseCase(repos.UserRepo, hasher),
DeleteUserUseCase: useCases.NewDeleteUserUseCase(repos.UserRepo),
+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
}
+2
View File
@@ -5,3 +5,5 @@ const BodyErrorMessage = "The request cannot be processed due to a mismatch in t
const IdUint32ErrorMessage = "The server expects the ID to be in the format of an unsigned 32-bit integer (uint32)."
const ArticleNotFoundErrorMessage = "Article not found"
const ArticleTitleErrorMessage = "Article title must be between 3 and 200 characters."
const RegistrationClosedErrorMessage = "You are not allowed to create a user. Log in or reset database."
const RegistrationFailedErrorMessage = "Registration failed. Please try again later."