package api import ( "net/http" "forge.cadoles.com/wpetit/hydra-webauthn/internal/storage" "github.com/go-chi/chi" "github.com/pkg/errors" "gitlab.com/wpetit/goweb/api" "gitlab.com/wpetit/goweb/logger" "gitlab.com/wpetit/goweb/middleware/container" ) type GenerateRegistrationLinkResponse struct { RegistrationLink *storage.RegistrationLink `json:"registrationLink"` } func GenerateRegistrationLink(w http.ResponseWriter, r *http.Request) { userID := chi.URLParam(r, "userID") ctx := r.Context() ctn := container.Must(ctx) storageSrv := storage.Must(ctn) registrationLink, err := storageSrv.User().GenerateRegistrationLink(ctx, userID) if err != nil { if errors.Is(err, storage.ErrNotFound) { logger.Warn(ctx, "user not found", logger.E(errors.WithStack(err)), logger.F("userID", userID)) api.ErrorResponse(w, http.StatusNotFound, api.ErrCodeNotFound, nil) return } logger.Error(ctx, "could not generate registration link", logger.E(errors.WithStack(err))) api.ErrorResponse(w, http.StatusInternalServerError, api.ErrCodeUnknownError, nil) return } response := GenerateRegistrationLinkResponse{ RegistrationLink: registrationLink, } api.DataResponse(w, http.StatusCreated, response) }