From 4cfa05d9fd10227d951e17a2b0dba718e652a5c8 Mon Sep 17 00:00:00 2001 From: Teajey <21069848+Teajey@users.noreply.github.com> Date: Thu, 23 May 2024 00:14:38 +1200 Subject: [PATCH] feat: login/registration messages --- auth/login_get.go | 28 ++++++++++++++++++++++++++++ auth/{login.go => login_post.go} | 0 auth/register.go | 9 +++++++++ main.go | 2 +- templates/login.go | 9 --------- templates/login.gotmpl | 9 +++++++-- templates/register.gotmpl | 6 +++++- 7 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 auth/login_get.go rename auth/{login.go => login_post.go} (100%) delete mode 100644 templates/login.go diff --git a/auth/login_get.go b/auth/login_get.go new file mode 100644 index 0000000..0add6f5 --- /dev/null +++ b/auth/login_get.go @@ -0,0 +1,28 @@ +package auth + +import ( + "lishwist/templates" + "log" + "net/http" +) + +type LoginGetProps struct { + SuccessfulRegistration bool +} + +func (auth *AuthMiddleware) Login(w http.ResponseWriter, r *http.Request) { + session, _ := auth.Store.Get(r, "lishwist_user") + successfulReg, ok := session.Values["successful_registration"].(bool) + if ok { + delete(session.Values, "successful_registration") + if err := session.Save(r, w); err != nil { + log.Println("Couldn't save session:", err) + http.Error(w, "Something went wrong. Error code: Zuko", http.StatusInternalServerError) + return + } + } + + templates.Execute(w, "login.gotmpl", LoginGetProps{ + SuccessfulRegistration: successfulReg, + }) +} diff --git a/auth/login.go b/auth/login_post.go similarity index 100% rename from auth/login.go rename to auth/login_post.go diff --git a/auth/register.go b/auth/register.go index 1f633a3..d275cce 100644 --- a/auth/register.go +++ b/auth/register.go @@ -1,6 +1,7 @@ package auth import ( + "log" "net/http" "lishwist/db" @@ -41,5 +42,13 @@ func (auth *AuthMiddleware) RegisterPost(w http.ResponseWriter, r *http.Request) return } + session, _ := auth.Store.Get(r, "lishwist_user") + session.Values["successful_registration"] = true + if err := session.Save(r, w); err != nil { + log.Println("Couldn't save session:", err) + http.Error(w, "Something went wrong. Error code: Zuko", http.StatusInternalServerError) + return + } + http.Redirect(w, r, "/", http.StatusSeeOther) } diff --git a/main.go b/main.go index 5670934..cba9957 100644 --- a/main.go +++ b/main.go @@ -32,7 +32,7 @@ func main() { publicMux.HandleFunc("GET /register", templates.Register) publicMux.HandleFunc("POST /register", authMiddleware.RegisterPost) - publicMux.HandleFunc("GET /", templates.Login) + publicMux.HandleFunc("GET /", authMiddleware.Login) publicMux.HandleFunc("POST /", authMiddleware.LoginPost) protectedMux.HandleFunc("GET /", ctx.Home) diff --git a/templates/login.go b/templates/login.go deleted file mode 100644 index 5d86132..0000000 --- a/templates/login.go +++ /dev/null @@ -1,9 +0,0 @@ -package templates - -import ( - "net/http" -) - -func Login(w http.ResponseWriter, r *http.Request) { - Execute(w, "login.gotmpl", nil) -} diff --git a/templates/login.gotmpl b/templates/login.gotmpl index c019dcf..f32ba41 100644 --- a/templates/login.gotmpl +++ b/templates/login.gotmpl @@ -2,7 +2,12 @@ {{end}} {{define "body"}} -
Registration successful. Now you can login.
+Your password will be stored in a safe, responsible manner; but don't trust my programming skills!
+Maybe use a password here that you don't use for important things...
+