mirror of
https://github.com/onsonr/sonr.git
synced 2025-03-10 21:09:11 +00:00
feat: Add validation for human verification slider sum in CreateProfile form
This commit is contained in:
parent
0e8b92e53d
commit
ce3d073abd
@ -2,6 +2,8 @@ package forms
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"errors"
|
||||
"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)
|
||||
}
|
||||
|
||||
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
|
||||
templ CreateProfile(action string, method string, data CreateProfileData) {
|
||||
<form action={ templ.SafeURL(action) } method={ method }>
|
||||
@ -35,7 +61,26 @@ templ CreateProfile(action string, method string, data CreateProfileData) {
|
||||
</div>
|
||||
</sl-input>
|
||||
@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">
|
||||
<sl-button href="/" outline>
|
||||
<sl-icon slot="prefix" name="arrow-left" library="sonr"></sl-icon>
|
||||
|
@ -24,6 +24,16 @@ func HandleRegisterView(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()
|
||||
handle := c.FormValue("handle")
|
||||
firstName := c.FormValue("first_name")
|
||||
|
Loading…
x
Reference in New Issue
Block a user