Skip to content

Commit

Permalink
ch 17-6: improve readability change variables names
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelClaumann committed Jun 14, 2024
1 parent 82e63e1 commit db469e3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
22 changes: 11 additions & 11 deletions cmd/web/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ type userLoginForm struct {
}

type updatePasswordForm struct {
Current string `form:"current"`
New string `form:"new"`
Confirmation string `form:"confirmation"`
validator.Validator `form:"-"`
CurrentPassword string `form:"current"`
NewPassword string `form:"new"`
NewPasswordConfirmation string `form:"confirmation"`
validator.Validator `form:"-"`
}

func (app *application) home(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -313,11 +313,11 @@ func (app *application) updatePasswordPost(w http.ResponseWriter, r *http.Reques
return
}

form.CheckField(validator.NotBlank(form.Current), "current", "This field cannot be blank")
form.CheckField(validator.NotBlank(form.New), "new", "This field cannot be blank")
form.CheckField(validator.NotBlank(form.Confirmation), "confirmation", "This field cannot be blank")
form.CheckField(validator.MinChars(form.New, 8), "new", "This field must be at least 8 characters long")
form.CheckField(validator.MinChars(form.Confirmation, 8), "confirmation", "This field must be at least 8 characters long")
form.CheckField(validator.NotBlank(form.CurrentPassword), "current", "This field cannot be blank")
form.CheckField(validator.NotBlank(form.NewPassword), "new", "This field cannot be blank")
form.CheckField(validator.NotBlank(form.NewPasswordConfirmation), "confirmation", "This field cannot be blank")
form.CheckField(validator.MinChars(form.NewPassword, 8), "new", "This field must be at least 8 characters long")
form.CheckField(validator.MinChars(form.NewPasswordConfirmation, 8), "confirmation", "This field must be at least 8 characters long")

if !form.Valid() {
data := app.newTemplateData(r)
Expand All @@ -326,13 +326,13 @@ func (app *application) updatePasswordPost(w http.ResponseWriter, r *http.Reques
return
}

if (form.New != form.Confirmation) || (form.New == form.Current) {
if (form.NewPassword != form.NewPasswordConfirmation) || (form.NewPassword == form.CurrentPassword) {
http.Redirect(w, r, "/account/password/update", http.StatusSeeOther)
return
}

id := app.sessionManager.GetInt(r.Context(), "authenticatedUserID")
err = app.users.UpdatePassword(id, form.Current, form.New)
err = app.users.UpdatePassword(id, form.CurrentPassword, form.NewPassword)
if err != nil {
http.Redirect(w, r, "/account/password/update", http.StatusSeeOther)
return
Expand Down
2 changes: 1 addition & 1 deletion internal/models/mocks/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (m *UserModel) Authenticate(email, password string) (int, error) {
return 0, models.ErrInvalidCredentials
}

func (m *UserModel) UpdatePassword(id int, current, new string) error {
func (m *UserModel) UpdatePassword(id int, currentPassword, newPassword string) error {
switch id {
case 1:
return models.ErrNoRecord
Expand Down
8 changes: 4 additions & 4 deletions internal/models/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type UserModelInterface interface {
Authenticate(email, password string) (int, error)
Exists(id int) (bool, error)
Get(id int) (*User, error)
UpdatePassword(id int, current, new string) error
UpdatePassword(id int, currentPassword, newPassword string) error
}

// Define a new User type. Notice how the field names and types align
Expand Down Expand Up @@ -115,13 +115,13 @@ func (m *UserModel) Authenticate(email, password string) (int, error) {
return id, nil
}

func (m *UserModel) UpdatePassword(id int, current, new string) error {
func (m *UserModel) UpdatePassword(id int, currentPassword, newPassword string) error {
user, err := m.Get(id)
if err != nil {
return err
}

err = bcrypt.CompareHashAndPassword(user.HashedPassword, []byte(current))
err = bcrypt.CompareHashAndPassword(user.HashedPassword, []byte(currentPassword))
if err != nil {
if errors.Is(err, bcrypt.ErrMismatchedHashAndPassword) {
return ErrInvalidCredentials
Expand All @@ -130,7 +130,7 @@ func (m *UserModel) UpdatePassword(id int, current, new string) error {
}
}

hashedPassword, err := bcrypt.GenerateFromPassword([]byte(new), 12)
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(newPassword), 12)
if err != nil {
return err
}
Expand Down

0 comments on commit db469e3

Please sign in to comment.