monorepo/cloud/maplefile-backend/internal/interface/http/auth/verify_email.go

59 lines
1.7 KiB
Go

// codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/interface/http/auth/verify_email.go
package auth
import (
"encoding/json"
"net/http"
"go.uber.org/zap"
svc_auth "codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/service/auth"
"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/pkg/httperror"
)
// VerifyEmailHandler handles email verification
type VerifyEmailHandler struct {
logger *zap.Logger
service svc_auth.VerifyEmailService
}
// NewVerifyEmailHandler creates a new verify email handler
func NewVerifyEmailHandler(
logger *zap.Logger,
service svc_auth.VerifyEmailService,
) *VerifyEmailHandler {
return &VerifyEmailHandler{
logger: logger.Named("VerifyEmailHandler"),
service: service,
}
}
// ServeHTTP handles the HTTP request
func (h *VerifyEmailHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Decode request
var req svc_auth.VerifyEmailRequestDTO
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
h.logger.Error("Failed to decode verify email request", zap.Error(err))
problem := httperror.NewBadRequestError("Invalid request payload. Expected JSON with 'code' field.").
WithInstance(r.URL.Path).
WithTraceID(httperror.ExtractRequestID(r))
httperror.RespondWithProblem(w, problem)
return
}
// Call service
resp, err := h.service.Execute(ctx, &req)
if err != nil {
h.logger.Error("Email verification failed", zap.Error(err))
// Service returns RFC 9457 errors, use RespondWithError to handle them
httperror.RespondWithError(w, r, err)
return
}
// Return success response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
}