Handlers: Add password confirmation validation to signup
- Server-side validation for password confirmation field - Display error message if passwords don't match - Complements client-side validation added in previous commitjocadbz
parent
ca4268dfec
commit
69a62f0ad5
|
|
@ -1,10 +1,10 @@
|
||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/gorilla/sessions"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"threadr/models"
|
"threadr/models"
|
||||||
"github.com/gorilla/sessions"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func SignupHandler(app *App) http.HandlerFunc {
|
func SignupHandler(app *App) http.HandlerFunc {
|
||||||
|
|
@ -14,6 +14,34 @@ func SignupHandler(app *App) http.HandlerFunc {
|
||||||
if r.Method == http.MethodPost {
|
if r.Method == http.MethodPost {
|
||||||
username := r.FormValue("username")
|
username := r.FormValue("username")
|
||||||
password := r.FormValue("password")
|
password := r.FormValue("password")
|
||||||
|
passwordConfirm := r.FormValue("password_confirm")
|
||||||
|
|
||||||
|
// Server-side validation for password confirmation
|
||||||
|
if password != passwordConfirm {
|
||||||
|
log.Printf("Password confirmation mismatch for user: %s", username)
|
||||||
|
data := struct {
|
||||||
|
PageData
|
||||||
|
Error string
|
||||||
|
}{
|
||||||
|
PageData: PageData{
|
||||||
|
Title: "ThreadR - Sign Up",
|
||||||
|
Navbar: "signup",
|
||||||
|
LoggedIn: false,
|
||||||
|
ShowCookieBanner: cookie == nil || cookie.Value != "accepted",
|
||||||
|
BasePath: app.Config.ThreadrDir,
|
||||||
|
StaticPath: app.Config.ThreadrDir + "/static",
|
||||||
|
CurrentURL: r.URL.Path,
|
||||||
|
},
|
||||||
|
Error: "Passwords do not match. Please try again.",
|
||||||
|
}
|
||||||
|
if err := app.Tmpl.ExecuteTemplate(w, "signup", data); err != nil {
|
||||||
|
log.Printf("Error executing template in SignupHandler: %v", err)
|
||||||
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
err := models.CreateUser(app.DB, username, password)
|
err := models.CreateUser(app.DB, username, password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error creating user: %v", err)
|
log.Printf("Error creating user: %v", err)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue