Skip to content

Commit 80fad86

Browse files
committed
finished snippetbox
1 parent 095035d commit 80fad86

File tree

5 files changed

+78
-5
lines changed

5 files changed

+78
-5
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@ start:
77
docker start $(DB)
88
go run ./cmd/web
99

10+
debug:
11+
docker start $(DB)
12+
go run ./cmd/web -debug
13+
1014
stop:
1115
docker stop $(DB)

cmd/web/handlers.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ func userSignupPost(app *config.Application) http.HandlerFunc {
148148
form.AddFieldError("email", "Email address is already in use")
149149
data := app.NewTemplateData(r)
150150
data.Form = form
151-
app.Render(w, http.StatusUnprocessableEntity, "signup.tmpl",
152-
data)
151+
app.Render(w, http.StatusUnprocessableEntity, "signup.tmpl", data)
153152
} else {
154153
app.ServerError(w, err)
155154
}
@@ -271,9 +270,9 @@ func accountView(app *config.Application) http.HandlerFunc {
271270
}
272271

273272
type accountPasswordUpdateForm struct {
274-
CurrentPassword string `form:"password"`
275-
NewPassword string `form:"password"`
276-
NewPasswordConfirmation string `form:"password"`
273+
CurrentPassword string `form:"currentPassword"`
274+
NewPassword string `form:"newPassword"`
275+
NewPasswordConfirmation string `form:"newPasswordConfirmation"`
277276
validator.Validator `form:"-"`
278277
}
279278

@@ -309,6 +308,23 @@ func accountPasswordUpdatePost(app *config.Application) http.HandlerFunc {
309308
return
310309
}
311310

311+
userID := app.SessionManager.GetInt(r.Context(), "authenticatedUserID")
312+
err = app.Users.PasswordUpdate(userID, form.CurrentPassword, form.NewPassword)
313+
if err != nil {
314+
if errors.Is(err, models.ErrInvalidCredentials) {
315+
form.AddFieldError("currentPassword", "Password is wrong")
316+
data := app.NewTemplateData(r)
317+
data.Form = form
318+
app.Render(w, http.StatusUnprocessableEntity, "password.tmpl", data)
319+
return
320+
}
321+
322+
app.ServerError(w, err)
323+
return
324+
}
325+
326+
app.SessionManager.Put(r.Context(), "flash", "You've changed your password!")
327+
http.Redirect(w, r, "/account/view", http.StatusSeeOther)
312328
}
313329
}
314330

internal/models/mocks/user.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,13 @@ func (m *UserModel) Get(id int) (*models.User, error) {
4444
}
4545
return nil, models.ErrNoRecord
4646
}
47+
48+
func (m *UserModel) PasswordUpdate(id int, currentPassword, newPassword string) error {
49+
if id == 1 {
50+
if currentPassword != "pa$$word" {
51+
return models.ErrInvalidCredentials
52+
}
53+
return nil
54+
}
55+
return models.ErrNoRecord
56+
}

internal/models/users.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type UserModelInterface interface {
1414
Authenticate(email, password string) (int, error)
1515
Exists(id int) (bool, error)
1616
Get(id int) (*User, error)
17+
PasswordUpdate(id int, currentPassword, newPassword string) error
1718
}
1819

1920
// Define a new User type. Notice how the field names and types align
@@ -113,3 +114,43 @@ func (m *UserModel) Get(id int) (*User, error) {
113114

114115
return &user, nil
115116
}
117+
118+
func (m *UserModel) PasswordUpdate(id int, currentPassword, newPassword string) error {
119+
var user User
120+
stmt := "SELECT id, name, email, hashed_password, created FROM users WHERE id = ?"
121+
err := m.DB.QueryRow(stmt, id).Scan(&user.ID, &user.Name, &user.Email, &user.HashedPassword, &user.Created)
122+
123+
if err != nil {
124+
if errors.Is(err, sql.ErrNoRows) {
125+
return ErrNoRecord
126+
} else {
127+
return err
128+
}
129+
}
130+
131+
err = bcrypt.CompareHashAndPassword(user.HashedPassword, []byte(currentPassword))
132+
if err != nil {
133+
if errors.Is(err, bcrypt.ErrMismatchedHashAndPassword) {
134+
return ErrInvalidCredentials
135+
} else {
136+
return err
137+
}
138+
}
139+
140+
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(newPassword), 12)
141+
if err != nil {
142+
return err
143+
}
144+
145+
stmt = "UPDATE users SET hashed_password = ? WHERE id = ?"
146+
_, err = m.DB.Exec(stmt, hashedPassword, id)
147+
if err != nil {
148+
if errors.Is(err, sql.ErrNoRows) {
149+
return ErrNoRecord
150+
} else {
151+
return err
152+
}
153+
}
154+
155+
return nil
156+
}

ui/html/pages/account.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
<th>Name</th>
1111
<th>Email</th>
1212
<th>Created</th>
13+
<th>Password</th>
1314
</tr>
1415
<tr>
1516
<td>{{.ID}}</td>
1617
<td>{{.Name}}</td>
1718
<td>{{.Email}}</td>
1819
<td>{{humanDate .Created}}</td>
20+
<td><a href='/account/password/update'>Change Password</a></td>
1921
</tr>
2022
</table>
2123
</div>

0 commit comments

Comments
 (0)