Skip to content

Commit

Permalink
Fixed registration form validation (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
reaper47 authored Sep 16, 2024
1 parent d0cef0b commit 184a534
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 43 deletions.
8 changes: 5 additions & 3 deletions internal/server/handlers_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log/slog"
"maps"
"net/http"
"net/mail"
"strconv"
"strings"
"time"
Expand All @@ -14,7 +15,6 @@ import (
"github.com/reaper47/recipya/internal/auth"
"github.com/reaper47/recipya/internal/models"
"github.com/reaper47/recipya/internal/templates"
"github.com/reaper47/recipya/internal/utils/regex"
"github.com/reaper47/recipya/web/components"
)

Expand Down Expand Up @@ -261,7 +261,8 @@ func (s *Server) loginPostHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
email := r.FormValue("email")
password := r.FormValue("password")
if !regex.Email.MatchString(email) || password == "" {
_, err := mail.ParseAddress(email)
if err != nil || password == "" {
w.Header().Set("HX-Trigger", models.NewErrorFormToast("Credentials are invalid.").Render())
w.WriteHeader(http.StatusBadRequest)
return
Expand Down Expand Up @@ -349,7 +350,8 @@ func (s *Server) registerPostHandler() http.HandlerFunc {
email := r.FormValue("email")
password := r.FormValue("password")

if !regex.Email.MatchString(email) || password != r.FormValue("password-confirm") {
_, err := mail.ParseAddress(email)
if err != nil || password != r.FormValue("password-confirm") {
w.Header().Set("HX-Trigger", models.NewErrorFormToast("User might be registered or password invalid.").Render())
w.WriteHeader(http.StatusUnprocessableEntity)
return
Expand Down
12 changes: 7 additions & 5 deletions internal/services/sqlite_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"log/slog"
"net/mail"
"net/url"
"os"
"path/filepath"
Expand All @@ -26,7 +27,6 @@ import (
"github.com/reaper47/recipya/internal/units"
"github.com/reaper47/recipya/internal/utils/duration"
"github.com/reaper47/recipya/internal/utils/extensions"
"github.com/reaper47/recipya/internal/utils/regex"
_ "modernc.org/sqlite" // Blank import to initialize the SQL driver.
)

Expand Down Expand Up @@ -1356,7 +1356,8 @@ func (s *SQLiteService) RecipeUser(recipeID int64) int64 {

// Register adds a new user to the store.
func (s *SQLiteService) Register(email string, hashedPassword auth.HashedPassword) (int64, error) {
if !regex.Email.MatchString(email) || hashedPassword == "" {
_, err := mail.ParseAddress(email)
if err != nil || hashedPassword == "" {
return -1, errors.New("credentials are invalid")
}

Expand All @@ -1367,7 +1368,7 @@ func (s *SQLiteService) Register(email string, hashedPassword auth.HashedPasswor
defer cancel()

var userID int64
err := s.DB.QueryRowContext(ctx, statements.InsertUser, email, hashedPassword).Scan(&userID)
err = s.DB.QueryRowContext(ctx, statements.InsertUser, email, hashedPassword).Scan(&userID)
return userID, err
}

Expand Down Expand Up @@ -2237,15 +2238,16 @@ func (s *SQLiteService) UpdateVideo(video uuid.UUID, durationSecs int) error {

// UserID gets the user's id from the email. It returns -1 if user not found.
func (s *SQLiteService) UserID(email string) int64 {
if !regex.Email.MatchString(email) {
_, err := mail.ParseAddress(email)
if err != nil {
return -1
}

ctx, cancel := context.WithTimeout(context.Background(), shortCtxTimeout)
defer cancel()

var id int64
err := s.DB.QueryRowContext(ctx, statements.SelectUserID, email).Scan(&id)
err = s.DB.QueryRowContext(ctx, statements.SelectUserID, email).Scan(&id)
if errors.Is(err, sql.ErrNoRows) {
return -1
}
Expand Down
3 changes: 0 additions & 3 deletions internal/utils/regex/regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ var Digit = regexp.MustCompile(`(\b\d+\s+\d+/\d+\b)|(\d+\.?/?\d*)`)
// DimensionPattern matches patterns representing dimensions.
var DimensionPattern = regexp.MustCompile(`(\d+)\s*x\s*(\d+).`)

// Email verifies whether an email address is valid.
var Email = regexp.MustCompile(`^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$`)

// Quantity detects quantities, i.e. 1ml, 1 ml, 1l and 1 l.
var Quantity = regexp.MustCompile(`(?i)\d+\s*((ml|l\b)(°[cf])?|°[cf])`)

Expand Down
34 changes: 2 additions & 32 deletions internal/utils/regex/regex_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package regex_test

import (
"github.com/reaper47/recipya/internal/utils/regex"
"regexp"
"testing"

"github.com/reaper47/recipya/internal/utils/regex"
)

func TestRegex_BeginsWithWord(t *testing.T) {
Expand Down Expand Up @@ -117,37 +118,6 @@ func TestRegex_DimensionPattern(t *testing.T) {
})
}

func TestRegex_Email(t *testing.T) {
t.Run("valid", func(t *testing.T) {
xs := []string{
"james@bond.com",
"hello@hello.ca",
"slave@ukrainia.ua",
"norway@rocks.no",
}
assertRegex(t, xs, regex.Email)
})

t.Run("invalid", func(t *testing.T) {
emails := []string{
"xyzGmail.com",
"@gmail.com",
"email",
"a@.com",
".com@",
"a@",
"norway@rocks",
}
for _, email := range emails {
t.Run("regex is invalid "+email, func(t *testing.T) {
if regex.Email.MatchString(email) {
t.Error("got true when want false")
}
})
}
})
}

func TestRegex_Quantity(t *testing.T) {
valid := []string{
"1ml",
Expand Down

0 comments on commit 184a534

Please sign in to comment.