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

+37 -2
View File
@@ -2,7 +2,10 @@ package useCases
import (
"RenewCMS/internal/domain/hash"
"RenewCMS/internal/domain/mail"
"RenewCMS/internal/domain/user"
"errors"
"fmt"
"time"
"github.com/google/uuid"
@@ -11,6 +14,8 @@ import (
type CreateUserUseCase struct {
userRepository user.Repository
hasher hash.Hasher
mailRepository mail.Repository
publicURL string
}
type CreateUserCommand struct {
@@ -19,11 +24,20 @@ type CreateUserCommand struct {
Email string
}
func NewCreateUserUseCase(userRepository user.Repository, hasher hash.Hasher) *CreateUserUseCase {
return &CreateUserUseCase{userRepository, hasher}
type MailValidationData struct {
Host string
VerificationCode string
}
func NewCreateUserUseCase(userRepository user.Repository, hasher hash.Hasher, mailRepository mail.Repository, publicURL string) *CreateUserUseCase {
return &CreateUserUseCase{userRepository, hasher, mailRepository, publicURL}
}
func (g *CreateUserUseCase) CreateUser(createUser CreateUserCommand) (user.User, error) {
if g.registrationClosed() {
return user.User{}, user.ErrRegistrationClosed
}
rawUuid := uuid.NewString()
hashedPassword, err := g.hasher.Hash(createUser.Password)
@@ -50,5 +64,26 @@ func (g *CreateUserUseCase) CreateUser(createUser CreateUserCommand) (user.User,
}
createdUser.VerificationCode = rawUuid
if err := g.mailRepository.Send(createdUser.Email, "mailValidation", MailValidationData{
Host: g.publicURL,
VerificationCode: rawUuid,
}); err != nil {
sendErr := fmt.Errorf("failed to send verification email: %w", err)
if deleteErr := g.userRepository.Delete(createdUser.ID); deleteErr != nil {
return user.User{}, errors.Join(sendErr, deleteErr)
}
return user.User{}, sendErr
}
return createdUser, nil
}
func (g *CreateUserUseCase) registrationClosed() bool {
for _, existingUser := range g.userRepository.GetAll() {
if existingUser.IsVerified {
return true
}
}
return false
}