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 @@
-