From bdf3a6798a3a2529a25c5554c7c3311678f670f1 Mon Sep 17 00:00:00 2001 From: Maeght Loan <37497007+loan-mgt@users.noreply.github.com> Date: Thu, 13 Jun 2024 20:17:42 +0000 Subject: [PATCH] init login request --- internal/controllers/indexController.go | 31 ++++++++++++++------- internal/types/template-data/index.go | 5 ++++ template/home.html.tmpl | 12 ++++----- template/index.html.tmpl | 6 ++++- template/logged.html.tmpl | 36 ++++++++++--------------- 5 files changed, 51 insertions(+), 39 deletions(-) create mode 100644 internal/types/template-data/index.go diff --git a/internal/controllers/indexController.go b/internal/controllers/indexController.go index 1a5f3b1..0044c80 100644 --- a/internal/controllers/indexController.go +++ b/internal/controllers/indexController.go @@ -4,25 +4,38 @@ import ( "log" "net/http" + templatedata "rcp/elite/internal/types/template-data" "rcp/elite/internal/utils" ) func IndexController(w http.ResponseWriter, r *http.Request) { - // Check if the 'stk' cookie is set + + data := templatedata.IndexData{ + Main: "logged", + } cookie, err := r.Cookie("stk") if err != nil { - err = utils.Templates.ExecuteTemplate(w, "index", nil) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + data.Main = "home" + + defaultCookie := http.Cookie{ + Name: "stk", + Value: "default_value_here", + MaxAge: 60 * 60 * 24 * 60, // 2 months in seconds (60 seconds * 60 minutes * 24 hours * 60 days) + HttpOnly: true, + Secure: true, + SameSite: http.SameSiteStrictMode, } - } + http.SetCookie(w, &defaultCookie) + log.Println("Cookie 'stk' set with default value") - // If the cookie is found, you can access its value with cookie.Value - log.Println("Cookie 'stk' found with value:", cookie.Value) + } else { + // If the cookie is found, you can access its value with cookie.Value + log.Println("Cookie 'stk' found with value:", cookie.Value) + } - err = utils.Templates.ExecuteTemplate(w, "logged", nil) + err = utils.Templates.ExecuteTemplate(w, "index", data) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } - + } diff --git a/internal/types/template-data/index.go b/internal/types/template-data/index.go new file mode 100644 index 0000000..282e58a --- /dev/null +++ b/internal/types/template-data/index.go @@ -0,0 +1,5 @@ +package templatedata + +type IndexData struct { + Main string +} diff --git a/template/home.html.tmpl b/template/home.html.tmpl index 8ae5a68..1050946 100644 --- a/template/home.html.tmpl +++ b/template/home.html.tmpl @@ -4,13 +4,11 @@ logo -
- - + + + - -
+ + {{ end }} diff --git a/template/index.html.tmpl b/template/index.html.tmpl index 364a25d..1170a9b 100644 --- a/template/index.html.tmpl +++ b/template/index.html.tmpl @@ -16,7 +16,11 @@ -{{ template "home" . }} +{{ if eq .Main "home" }} + {{ template "home" . }} +{{ else if eq .Main "logged" }} + {{ template "logged" . }} +{{ end }} diff --git a/template/logged.html.tmpl b/template/logged.html.tmpl index 87c4e4e..c3ebe28 100644 --- a/template/logged.html.tmpl +++ b/template/logged.html.tmpl @@ -1,24 +1,16 @@ {{ define "logged" }} - - - - - - - RCP Game - - - - - - - - - - -{{ template "home" . }} - - - - +
+
+ logo +
+ +
+ + + + +
+
{{ end }} \ No newline at end of file