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,66 +1,94 @@
|
||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"github.com/gorilla/sessions"
|
||||||
"net/http"
|
"log"
|
||||||
"threadr/models"
|
"net/http"
|
||||||
"github.com/gorilla/sessions"
|
"threadr/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SignupHandler(app *App) http.HandlerFunc {
|
func SignupHandler(app *App) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
session := r.Context().Value("session").(*sessions.Session)
|
session := r.Context().Value("session").(*sessions.Session)
|
||||||
cookie, _ := r.Cookie("threadr_cookie_banner")
|
cookie, _ := r.Cookie("threadr_cookie_banner")
|
||||||
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")
|
||||||
err := models.CreateUser(app.DB, username, password)
|
passwordConfirm := r.FormValue("password_confirm")
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error creating user: %v", err)
|
// Server-side validation for password confirmation
|
||||||
data := struct {
|
if password != passwordConfirm {
|
||||||
PageData
|
log.Printf("Password confirmation mismatch for user: %s", username)
|
||||||
Error string
|
data := struct {
|
||||||
}{
|
PageData
|
||||||
PageData: PageData{
|
Error string
|
||||||
Title: "ThreadR - Sign Up",
|
}{
|
||||||
Navbar: "signup",
|
PageData: PageData{
|
||||||
LoggedIn: false,
|
Title: "ThreadR - Sign Up",
|
||||||
ShowCookieBanner: cookie == nil || cookie.Value != "accepted",
|
Navbar: "signup",
|
||||||
BasePath: app.Config.ThreadrDir,
|
LoggedIn: false,
|
||||||
StaticPath: app.Config.ThreadrDir + "/static",
|
ShowCookieBanner: cookie == nil || cookie.Value != "accepted",
|
||||||
CurrentURL: r.URL.Path,
|
BasePath: app.Config.ThreadrDir,
|
||||||
},
|
StaticPath: app.Config.ThreadrDir + "/static",
|
||||||
Error: "An error occurred during sign up. Please try again.",
|
CurrentURL: r.URL.Path,
|
||||||
}
|
},
|
||||||
if err := app.Tmpl.ExecuteTemplate(w, "signup", data); err != nil {
|
Error: "Passwords do not match. Please try again.",
|
||||||
log.Printf("Error executing template in SignupHandler: %v", err)
|
}
|
||||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
if err := app.Tmpl.ExecuteTemplate(w, "signup", data); err != nil {
|
||||||
return
|
log.Printf("Error executing template in SignupHandler: %v", err)
|
||||||
}
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
http.Redirect(w, r, app.Config.ThreadrDir+"/login/", http.StatusFound)
|
return
|
||||||
return
|
}
|
||||||
}
|
|
||||||
data := struct {
|
err := models.CreateUser(app.DB, username, password)
|
||||||
PageData
|
if err != nil {
|
||||||
Error string
|
log.Printf("Error creating user: %v", err)
|
||||||
}{
|
data := struct {
|
||||||
PageData: PageData{
|
PageData
|
||||||
Title: "ThreadR - Sign Up",
|
Error string
|
||||||
Navbar: "signup",
|
}{
|
||||||
LoggedIn: session.Values["user_id"] != nil,
|
PageData: PageData{
|
||||||
ShowCookieBanner: cookie == nil || cookie.Value != "accepted",
|
Title: "ThreadR - Sign Up",
|
||||||
BasePath: app.Config.ThreadrDir,
|
Navbar: "signup",
|
||||||
StaticPath: app.Config.ThreadrDir + "/static",
|
LoggedIn: false,
|
||||||
CurrentURL: r.URL.Path,
|
ShowCookieBanner: cookie == nil || cookie.Value != "accepted",
|
||||||
},
|
BasePath: app.Config.ThreadrDir,
|
||||||
Error: "",
|
StaticPath: app.Config.ThreadrDir + "/static",
|
||||||
}
|
CurrentURL: r.URL.Path,
|
||||||
if err := app.Tmpl.ExecuteTemplate(w, "signup", data); err != nil {
|
},
|
||||||
log.Printf("Error executing template in SignupHandler: %v", err)
|
Error: "An error occurred during sign up. Please try again.",
|
||||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
}
|
||||||
return
|
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
|
||||||
|
}
|
||||||
|
http.Redirect(w, r, app.Config.ThreadrDir+"/login/", http.StatusFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data := struct {
|
||||||
|
PageData
|
||||||
|
Error string
|
||||||
|
}{
|
||||||
|
PageData: PageData{
|
||||||
|
Title: "ThreadR - Sign Up",
|
||||||
|
Navbar: "signup",
|
||||||
|
LoggedIn: session.Values["user_id"] != nil,
|
||||||
|
ShowCookieBanner: cookie == nil || cookie.Value != "accepted",
|
||||||
|
BasePath: app.Config.ThreadrDir,
|
||||||
|
StaticPath: app.Config.ThreadrDir + "/static",
|
||||||
|
CurrentURL: r.URL.Path,
|
||||||
|
},
|
||||||
|
Error: "",
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue