diff options
-rw-r--r-- | deleteacc.go | 38 | ||||
-rw-r--r-- | handler.go | 2 | ||||
-rw-r--r-- | main.go | 2 | ||||
-rw-r--r-- | tpls.go | 8 | ||||
-rw-r--r-- | tpls/reallydelete.tpl | 11 |
5 files changed, 57 insertions, 4 deletions
diff --git a/deleteacc.go b/deleteacc.go new file mode 100644 index 0000000..641d3c9 --- /dev/null +++ b/deleteacc.go @@ -0,0 +1,38 @@ +package main + +import ( + "github.com/gorilla/sessions" + "kch42.de/gostuff/mailremind/model" + "log" + "net/http" +) + +type reallydeleteTpldata struct { + OK bool +} + +func deleteask(user model.User, sess *sessions.Session, req *http.Request) interface{} { + return &reallydeleteTpldata{user != nil} +} + +func deleteacc(user model.User, sess *sessions.Session, req *http.Request) interface{} { + outdata := &msgTpldata{Title: "Delete Account"} + + if user == nil { + outdata.Class = "error" + outdata.Msg = "You need to be logged in to do that" + return outdata + } + + if err := user.Delete(); err != nil { + log.Printf("Error while deleting account: %s", err) + outdata.Class = "error" + outdata.Msg = "An error occurred during deletion. Please contact support, if this happens again." + return outdata + } + + delete(sess.Values, "uid") + outdata.Class = "success" + outdata.Msg = "Account deleted." + return outdata +} @@ -59,7 +59,7 @@ func mkHttpHandler(h Handler, tpl *template.Template) http.HandlerFunc { } if err := tpl.Execute(rw, outdata); err != nil { - log.Printf("Error executing template %s: %s", tpl.Name, err) + log.Printf("Error executing template %s: %s", tpl.Name(), err) } } } @@ -81,6 +81,8 @@ func main() { router.HandleFunc("/login", mkHttpHandler(login, tplLogin)) router.HandleFunc("/logincheck", mkHttpHandler(logincheck, tplMsg)) router.HandleFunc("/logout", mkHttpHandler(logout, tplMsg)) + router.HandleFunc("/delete-acc/yes", mkHttpHandler(deleteacc, tplMsg)) + router.HandleFunc("/delete-acc", mkHttpHandler(deleteask, tplReallyDelete)) http.Handle("/", router) @@ -17,9 +17,10 @@ func loadTpl(tplpath, name string) *template.Template { } var ( - tplRegister *template.Template - tplMsg *template.Template - tplLogin *template.Template + tplRegister *template.Template + tplMsg *template.Template + tplLogin *template.Template + tplReallyDelete *template.Template ) func initTpls() { @@ -31,6 +32,7 @@ func initTpls() { tplRegister = loadTpl(tplpath, "register") tplMsg = loadTpl(tplpath, "msg") tplLogin = loadTpl(tplpath, "login") + tplReallyDelete = loadTpl(tplpath, "reallydelete") } type msgTpldata struct { diff --git a/tpls/reallydelete.tpl b/tpls/reallydelete.tpl new file mode 100644 index 0000000..82afcf1 --- /dev/null +++ b/tpls/reallydelete.tpl @@ -0,0 +1,11 @@ +{{define "title"}}Delete Account{{end}} + +{{define "content"}} + {{if .OK}} + <a href="/delete-acc/yes">Click here if you really want to delete your account</a> + {{else}} + <div class="error"> + You need to be logged in to do that + </div> + {{end}} +{{end}}
\ No newline at end of file |