ch11.3
This commit is contained in:
@@ -19,6 +19,13 @@ type snippetCreateForm struct {
|
||||
validator.Validator `form:"-"`
|
||||
}
|
||||
|
||||
type userSignupForm struct {
|
||||
Name string `form:"name"`
|
||||
Email string `form:"email"`
|
||||
Password string `form:"password"`
|
||||
validator.Validator `form:"-"`
|
||||
}
|
||||
|
||||
func (app *application) render(w http.ResponseWriter, status int, page string, data *templateData) {
|
||||
ts, ok := app.templateCache[page]
|
||||
if !ok {
|
||||
@@ -119,7 +126,41 @@ func (app *application) snippetCreatePost(w http.ResponseWriter, r *http.Request
|
||||
}
|
||||
|
||||
func (app *application) userSignupPost(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "Create a new user...")
|
||||
var form userSignupForm
|
||||
err := app.decodePostForm(r, &form)
|
||||
if err != nil {
|
||||
app.clientError(w, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
form.CheckField(validator.NotBlank(form.Name), "name", "This field cannot be blank")
|
||||
form.CheckField(validator.NotBlank(form.Email), "email", "This field cannot be blank")
|
||||
form.CheckField(validator.Matches(form.Email, validator.EmailRX), "email", "This field must be a valid email address")
|
||||
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 character long")
|
||||
|
||||
if !form.Valid() {
|
||||
data := app.newTemplateData(r)
|
||||
data.Form = form
|
||||
app.render(w, http.StatusUnprocessableEntity, "signup.tmpl", data)
|
||||
return
|
||||
}
|
||||
|
||||
err = app.users.Insert(form.Name, form.Email, form.Password)
|
||||
if err != nil {
|
||||
if errors.Is(err, models.ErrDuplicateEmail) {
|
||||
form.AddFieldError("email", "Email address already in use")
|
||||
|
||||
data := app.newTemplateData(r)
|
||||
data.Form = form
|
||||
app.render(w, http.StatusUnprocessableEntity, "signup.tmpl", data)
|
||||
} else {
|
||||
app.serverError(w, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
app.sessionManager.Put(r.Context(), "flash", "Your singup was successful. Please log in.")
|
||||
http.Redirect(w, r, "/user/login", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func (app *application) userLoginPost(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -131,7 +172,9 @@ func (app *application) userLogoutPost(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (app *application) userSignup(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "Display a HTML for signing up a new user...")
|
||||
data := app.newTemplateData(r)
|
||||
data.Form = userSignupForm{}
|
||||
app.render(w, http.StatusOK, "signup.tmpl", data)
|
||||
}
|
||||
|
||||
func (app *application) userLogin(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -24,7 +24,7 @@ func (app *application) notFound(w http.ResponseWriter) {
|
||||
app.clientError(w, http.StatusNotFound)
|
||||
}
|
||||
|
||||
func (app *application) decodePostForm(r *http.Request, snippetForm *snippetCreateForm) error {
|
||||
func (app *application) decodePostForm(r *http.Request, snippetForm any) error {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user