feat: Add validation for human verification slider sum in CreateProfile form

This commit is contained in:
Prad Nukala 2024-12-09 17:36:09 -05:00
parent 0e8b92e53d
commit ce3d073abd
2 changed files with 56 additions and 1 deletions

View File

@ -2,6 +2,8 @@ package forms
import ( import (
"fmt" "fmt"
"strconv"
"errors"
"github.com/onsonr/sonr/pkg/blocks/layout" "github.com/onsonr/sonr/pkg/blocks/layout"
) )
@ -15,6 +17,30 @@ func (d CreateProfileData) IsHumanLabel() string {
return fmt.Sprintf("What is %d + %d?", d.FirstNumber, d.LastNumber) return fmt.Sprintf("What is %d + %d?", d.FirstNumber, d.LastNumber)
} }
func ValidateCreateProfileForm(formData map[string][]string) error {
// Validate ishuman slider
ishumanValues := formData["is_human"]
if len(ishumanValues) == 0 {
return errors.New("human verification is required")
}
ishumanSum, err := strconv.Atoi(ishumanValues[0])
if err != nil {
return errors.New("invalid human verification value")
}
// Get the expected sum from the form data
firstNum, _ := strconv.Atoi(formData["first_number"][0])
lastNum, _ := strconv.Atoi(formData["last_number"][0])
expectedSum := firstNum + lastNum
if ishumanSum != expectedSum {
return errors.New("incorrect sum for human verification")
}
return nil
}
// ProfileForm is a standard form styled like a card // ProfileForm is a standard form styled like a card
templ CreateProfile(action string, method string, data CreateProfileData) { templ CreateProfile(action string, method string, data CreateProfileData) {
<form action={ templ.SafeURL(action) } method={ method }> <form action={ templ.SafeURL(action) } method={ method }>
@ -35,7 +61,26 @@ templ CreateProfile(action string, method string, data CreateProfileData) {
</div> </div>
</sl-input> </sl-input>
@layout.Spacer() @layout.Spacer()
<sl-range name="is_human" label={ data.IsHumanLabel() } help-text="Prove you are a human." min="0" max="9" step="1"></sl-range> <sl-range
name="is_human"
label={ data.IsHumanLabel() }
help-text="Prove you are a human."
min="0"
max={ fmt.Sprint(data.FirstNumber + data.LastNumber) }
step="1"
onchange="validateSum(this)"
></sl-range>
<script>
function validateSum(slider) {
const expectedSum = { fmt.Sprint(data.FirstNumber + data.LastNumber) };
const value = parseInt(slider.value);
if (value !== expectedSum) {
slider.setCustomValidity("Please select the correct sum");
} else {
slider.setCustomValidity("");
}
}
</script>
<div slot="footer"> <div slot="footer">
<sl-button href="/" outline> <sl-button href="/" outline>
<sl-icon slot="prefix" name="arrow-left" library="sonr"></sl-icon> <sl-icon slot="prefix" name="arrow-left" library="sonr"></sl-icon>

View File

@ -24,6 +24,16 @@ func HandleRegisterView(c echo.Context) error {
} }
func HandleRegisterStart(c echo.Context) error { func HandleRegisterStart(c echo.Context) error {
// Validate the form submission
formData := make(map[string][]string)
for key, values := range c.Request().Form {
formData[key] = values
}
if err := forms.ValidateCreateProfileForm(formData); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
challenge, _ := protocol.CreateChallenge() challenge, _ := protocol.CreateChallenge()
handle := c.FormValue("handle") handle := c.FormValue("handle")
firstName := c.FormValue("first_name") firstName := c.FormValue("first_name")