aboutsummaryrefslogtreecommitdiffstats
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go38
1 files changed, 37 insertions, 1 deletions
diff --git a/main.go b/main.go
index 86fd969..10815a5 100644
--- a/main.go
+++ b/main.go
@@ -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)
}