|
|
|
@ -291,7 +291,7 @@ func handleUserSignupGet(tc *TemplateCache, sm *scs.SessionManager) http.Handler
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func handleUserSignupPost(logger *slog.Logger, tc *TemplateCache, fd *form.Decoder, sm *scs.SessionManager) http.Handler {
|
|
|
|
|
func handleUserSignupPost(logger *slog.Logger, tc *TemplateCache, fd *form.Decoder, sm *scs.SessionManager, userService *model.UserService) http.Handler {
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
|
|
// Check that the provided name, email address and password are not blank.
|
|
|
|
@ -319,12 +319,30 @@ func handleUserSignupPost(logger *slog.Logger, tc *TemplateCache, fd *form.Decod
|
|
|
|
|
form.CheckField(validator.NotBlank(form.Password), "password", "this field cannot be blank")
|
|
|
|
|
form.CheckField(validator.MinChars(form.Password, 8), "password", "this field must be at least 8 characters long")
|
|
|
|
|
|
|
|
|
|
// Todo Email allready in use validation
|
|
|
|
|
|
|
|
|
|
if !form.Valid() {
|
|
|
|
|
data := newTemplateData(r, sm)
|
|
|
|
|
data.Form = form
|
|
|
|
|
renderTemplate(w, r, tc, http.StatusUnprocessableEntity, "signup.go.tmpl", data)
|
|
|
|
|
}
|
|
|
|
|
fmt.Fprintln(w, "Creating new user")
|
|
|
|
|
|
|
|
|
|
_, err = userService.Insert(form.Name, form.Email, form.Password)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, model.ErrDuplicateEmail) {
|
|
|
|
|
form.AddFieldError("email", "Email is already in use")
|
|
|
|
|
|
|
|
|
|
data := newTemplateData(r, sm)
|
|
|
|
|
data.Form = form
|
|
|
|
|
renderTemplate(w, r, tc, http.StatusUnprocessableEntity, "signup.go.tmpl", data)
|
|
|
|
|
} else {
|
|
|
|
|
serverError(w, r, err)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sm.Put(r.Context(), "flash", "Your signup was successful. Please log in.")
|
|
|
|
|
http.Redirect(w, r, "/user/login", http.StatusSeeOther)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|