diff options
| author | Thibaut Horel <thibaut.horel@gmail.com> | 2018-12-31 09:05:29 -0500 |
|---|---|---|
| committer | Thibaut Horel <thibaut.horel@gmail.com> | 2018-12-31 09:05:29 -0500 |
| commit | 3b49837d167e8770f1054457e172c36109169e51 (patch) | |
| tree | 9ae844c7851f3f35aea677ebf4a8f80924f76045 /main.go | |
| parent | 6ae97fe4d7c4fa1c80571d41c356453199041067 (diff) | |
| download | id-3b49837d167e8770f1054457e172c36109169e51.tar.gz | |
Add password change feature
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 38 |
1 files changed, 37 insertions, 1 deletions
@@ -59,7 +59,6 @@ func (app *App) rootHandler(w http.ResponseWriter, r *http.Request) { func (app *App) loginHandler(w http.ResponseWriter, r *http.Request) { if _, ok := app.validate(r); ok { http.Redirect(w, r, "/", http.StatusSeeOther) - return } else if r.Method == http.MethodPost { username := r.FormValue("username") hash := md5hex([]byte(r.FormValue("password"))) @@ -95,6 +94,42 @@ func (app *App) loginHandler(w http.ResponseWriter, r *http.Request) { } } +type Flash struct { + Type string + Value string +} + +func (app *App) passwordHandler(w http.ResponseWriter, r *http.Request) { + if s, ok := app.validate(r); !ok { + http.Redirect(w, r, "/login", http.StatusSeeOther) + return + } else if r.Method == http.MethodGet { + app.Template.ExecuteTemplate(w, "password.tmpl", Flash{}) + } else if r.Method == http.MethodPost { + password := r.FormValue("password") + confirm := r.FormValue("confirm") + if password != "" && password == confirm { + hash := md5hex([]byte(password)) + app.ChangePassword(s.UserId, hash) + app.Template.ExecuteTemplate(w, "password.tmpl", Flash{ + "success", + "Mot de passe enregistré", + }) + } else { + var bad string + if password != confirm { + bad = "Les deux mots de passe ne coïncident pas" + } else if password == "" { + bad = "Le mot de passe est vide" + } + app.Template.ExecuteTemplate(w, "password.tmpl", Flash{ + "danger", + bad, + }) + } + } +} + func (app *App) logoutHandler(w http.ResponseWriter, r *http.Request) { if s, ok := app.validate(r); ok { // should we save old sessions in another table? @@ -142,6 +177,7 @@ func main() { http.HandleFunc("/login", app.loginHandler) http.HandleFunc("/logout", app.logoutHandler) http.HandleFunc("/", app.rootHandler) + http.HandleFunc("/password", app.passwordHandler) if err := http.ListenAndServe(*address, logMux(http.DefaultServeMux)); err != nil { panic(err) } |
