From 4bc0b9304d60db854cb11b8ba5c5e908566da9a6 Mon Sep 17 00:00:00 2001 From: bonfy Date: Thu, 11 Oct 2018 11:12:06 +0800 Subject: [PATCH] 10-Email-Support add reset_password page --- controller/home.go | 39 +++++++++++++++++++++++++++ templates/content/reset_password.html | 17 ++++++++++++ vm/reset_password.go | 32 ++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 templates/content/reset_password.html create mode 100644 vm/reset_password.go diff --git a/controller/home.go b/controller/home.go index 01a4092..7b8ee89 100644 --- a/controller/home.go +++ b/controller/home.go @@ -24,6 +24,7 @@ func (h home) registerRoutes() { r.HandleFunc("/profile_edit", middleAuth(profileEditHandler)) r.HandleFunc("/explore", middleAuth(exploreHandler)) r.HandleFunc("/reset_password_request", resetPasswordRequestHandler) + r.HandleFunc("/reset_password/{token}", resetPasswordHandler) r.HandleFunc("/", middleAuth(indexHandler)) http.Handle("/", r) @@ -233,3 +234,41 @@ func resetPasswordRequestHandler(w http.ResponseWriter, r *http.Request) { } } } + +func resetPasswordHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + token := vars["token"] + username, err := vm.CheckToken(token) + if err != nil { + w.Write([]byte("The token is no longer valid, please go to the login page.")) + } + + tpName := "reset_password.html" + vop := vm.ResetPasswordViewModelOp{} + v := vop.GetVM(token) + + if r.Method == http.MethodGet { + templates[tpName].Execute(w, &v) + } + + if r.Method == http.MethodPost { + log.Println("Reset password for ", username) + r.ParseForm() + pwd1 := r.Form.Get("pwd1") + pwd2 := r.Form.Get("pwd2") + + errs := checkResetPassword(pwd1, pwd2) + v.AddError(errs...) + + if len(v.Errs) > 0 { + templates[tpName].Execute(w, &v) + } else { + if err := vm.ResetUserPassword(username, pwd1); err != nil { + log.Println("reset User password error:", err) + w.Write([]byte("Error update user password in database")) + return + } + http.Redirect(w, r, "/login", http.StatusSeeOther) + } + } +} diff --git a/templates/content/reset_password.html b/templates/content/reset_password.html new file mode 100644 index 0000000..57c69fa --- /dev/null +++ b/templates/content/reset_password.html @@ -0,0 +1,17 @@ +{{define "content"}} +

Register

+
+

+

+

+
+ + + {{if .Errs}} + + {{end}} +{{end}} diff --git a/vm/reset_password.go b/vm/reset_password.go new file mode 100644 index 0000000..ed58b24 --- /dev/null +++ b/vm/reset_password.go @@ -0,0 +1,32 @@ +package vm + +import ( + "github.com/bonfy/go-mega-code/model" +) + +// ResetPasswordViewModel struct +type ResetPasswordViewModel struct { + LoginViewModel + Token string +} + +// ResetPasswordViewModelOp struct +type ResetPasswordViewModelOp struct{} + +// GetVM func +func (ResetPasswordViewModelOp) GetVM(token string) ResetPasswordViewModel { + v := ResetPasswordViewModel{} + v.SetTitle("Reset Password") + v.Token = token + return v +} + +// CheckToken func +func CheckToken(tokenString string) (string, error) { + return model.CheckToken(tokenString) +} + +// ResetUserPassword func +func ResetUserPassword(username, password string) error { + return model.UpdatePassword(username, password) +}