Skip to content

Commit

Permalink
10-Email-Support add reset_password page
Browse files Browse the repository at this point in the history
  • Loading branch information
bonfy committed Oct 11, 2018
1 parent 3e6471f commit 4bc0b93
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
39 changes: 39 additions & 0 deletions controller/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
}
17 changes: 17 additions & 0 deletions templates/content/reset_password.html
Original file line number Diff line number Diff line change
@@ -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}}
32 changes: 32 additions & 0 deletions vm/reset_password.go
Original file line number Diff line number Diff line change
@@ -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.