Skip to content

Commit 095035d

Browse files
committed
working on adding password change
1 parent 5c90233 commit 095035d

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

cmd/web/handlers.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,48 @@ func accountView(app *config.Application) http.HandlerFunc {
270270
}
271271
}
272272

273+
type accountPasswordUpdateForm struct {
274+
CurrentPassword string `form:"password"`
275+
NewPassword string `form:"password"`
276+
NewPasswordConfirmation string `form:"password"`
277+
validator.Validator `form:"-"`
278+
}
279+
280+
func accountPasswordUpdate(app *config.Application) http.HandlerFunc {
281+
return func(w http.ResponseWriter, r *http.Request) {
282+
data := app.NewTemplateData(r)
283+
data.Form = accountPasswordUpdateForm{}
284+
app.Render(w, http.StatusOK, "password.tmpl", data)
285+
}
286+
}
287+
288+
func accountPasswordUpdatePost(app *config.Application) http.HandlerFunc {
289+
return func(w http.ResponseWriter, r *http.Request) {
290+
var form accountPasswordUpdateForm
291+
292+
err := app.DecodePostForm(r, &form)
293+
if err != nil {
294+
app.ClientError(w, http.StatusBadRequest)
295+
return
296+
}
297+
298+
form.CheckField(validator.NotBlank(form.CurrentPassword), "currentPassword", "This field cannot be blank")
299+
form.CheckField(validator.NotBlank(form.NewPassword), "newPassword", "This field cannot be blank")
300+
form.CheckField(validator.NotBlank(form.NewPasswordConfirmation), "newPasswordConfirmation", "This field cannot be blank")
301+
form.CheckField(validator.MinChars(form.CurrentPassword, 8), "currentPassword", "This field must be at least 8 characters")
302+
form.CheckField(validator.MinChars(form.NewPassword, 8), "newPassword", "This field must must be at least 8 characters")
303+
form.CheckField(validator.MinChars(form.NewPasswordConfirmation, 8), "newPasswordConfirmation", "This field must be at least 8 characters")
304+
305+
if !form.Valid() {
306+
data := app.NewTemplateData(r)
307+
data.Form = form
308+
app.Render(w, http.StatusUnprocessableEntity, "password.tmpl", data)
309+
return
310+
}
311+
312+
}
313+
}
314+
273315
func ping(w http.ResponseWriter, r *http.Request) {
274316
w.Write([]byte("OK"))
275317
}

cmd/web/routes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ func routes(app *config.Application) func() http.Handler {
3838
protected := dynamic.Append(requireAuthentication(app))
3939

4040
router.Handler(http.MethodGet, "/account/view", protected.ThenFunc(accountView(app)))
41+
router.Handler(http.MethodGet, "/account/password/update", protected.ThenFunc(accountPasswordUpdate(app)))
42+
router.Handler(http.MethodPost, "/account/password/update", protected.ThenFunc(accountPasswordUpdatePost(app)))
4143
router.Handler(http.MethodGet, "/snippet/create", protected.ThenFunc(snippetCreate(app)))
4244
router.Handler(http.MethodPost, "/snippet/create", protected.ThenFunc(snippetCreatePost(app)))
4345
router.Handler(http.MethodPost, "/user/logout", protected.ThenFunc(userLogoutPost(app)))

ui/html/pages/password.tmpl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{{define "title"}}Change Password{{end}}
2+
3+
{{define "main"}}
4+
<form action='/account/password/update' method='POST' novalidate>
5+
<input type='hidden' name='csrf_token' value='{{.CSRFToken}}'>
6+
{{range .Form.NonFieldErrors}}
7+
<div class='error'>{{.}}</div>
8+
{{end}}
9+
<div>
10+
<label>Current Password:</label>
11+
{{with .Form.FieldErrors.currentPassword}}
12+
<label class='error'>{{.}}</label>
13+
{{end}}
14+
<input type='password' name='currentPassword'>
15+
</div>
16+
<div>
17+
<label>New Password:</label>
18+
{{with .Form.FieldErrors.newPassword}}
19+
<label class='error'>{{.}}</label>
20+
{{end}}
21+
<input type='password' name='newPassword'>
22+
</div>
23+
<div>
24+
<label>New Password Confirmation:</label>
25+
{{with .Form.FieldErrors.newPasswordConfirmation}}
26+
<label class='error'>{{.}}</label>
27+
{{end}}
28+
<input type='password' name='newPasswordConfirmation'>
29+
</div>
30+
<div>
31+
<input type='submit' value='Update'>
32+
</div>
33+
</form>
34+
{{end}}

0 commit comments

Comments
 (0)