Skip to content
Permalink
Browse files

10-Email-Support add reset_password page

  • Loading branch information...
bonfy committed Oct 11, 2018
1 parent 3e6471f commit 4bc0b9304d60db854cb11b8ba5c5e908566da9a6
Showing with 88 additions and 0 deletions.
  1. +39 −0 controller/home.go
  2. +17 −0 templates/content/reset_password.html
  3. +32 −0 vm/reset_password.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)
}
}
}
@@ -0,0 +1,17 @@
{{define "content"}}
<h1>Register</h1>
<form action="/reset_password/{{.Token}}" method="post" name="reset_password">
<p><input type="password" name="pwd1" value="" placeholder="Password"></p>
<p><input type="password" name="pwd2" value="" placeholder="Confirm Password"></p>
<p><input type="submit" name="submit" value="Reset"></p>
</form>


{{if .Errs}}
<ul>
{{range .Errs}}
<li>{{.}}</li>
{{end}}
</ul>
{{end}}
{{end}}
@@ -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)
}

0 comments on commit 4bc0b93

Please sign in to comment.
You can’t perform that action at this time.