diff --git a/main.go b/main.go index 44836b0..fe001a3 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "encoding/base64" "encoding/json" "html/template" "log" @@ -35,6 +36,11 @@ func handleStatic(w http.ResponseWriter, r *http.Request) { name := strings.TrimPrefix(path, "/") + if name == "index.html" && getProfile(r) != nil { + http.Redirect(w, r, "/home.html", http.StatusSeeOther) + return + } + if strings.HasSuffix(name, ".html") { t := templates.Lookup(name) if t == nil { @@ -42,13 +48,20 @@ func handleStatic(w http.ResponseWriter, r *http.Request) { return } w.Header().Set("Content-Type", "text/html") - t.Execute(w, envMap()) + t.Execute(w, templateData(r)) return } http.ServeFile(w, r, filepath.Join("static", name)) } +func templateData(r *http.Request) map[string]any { + return map[string]any{ + "env": envMap(), + "profile": getProfile(r), + } +} + func envMap() map[string]string { m := map[string]string{} for _, e := range os.Environ() { @@ -59,6 +72,33 @@ func envMap() map[string]string { return m } +func getProfile(r *http.Request) map[string]any { + cookie, err := r.Cookie("profile") + if err != nil { + return nil + } + data, err := base64.RawURLEncoding.DecodeString(cookie.Value) + if err != nil { + return nil + } + var profile map[string]any + if json.Unmarshal(data, &profile) != nil { + return nil + } + return profile +} + +func setProfile(w http.ResponseWriter, profile map[string]any) { + data, _ := json.Marshal(profile) + http.SetCookie(w, &http.Cookie{ + Name: "profile", + Value: base64.RawURLEncoding.EncodeToString(data), + Path: "/", + HttpOnly: true, + SameSite: http.SameSiteLaxMode, + }) +} + func handleGoogleCallback(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) @@ -78,12 +118,12 @@ func handleGoogleCallback(w http.ResponseWriter, r *http.Request) { return } - response := map[string]any{ + profile := map[string]any{ "email": payload.Claims["email"], "name": payload.Claims["name"], "picture": payload.Claims["picture"], } - w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(response) + setProfile(w, profile) + http.Redirect(w, r, "/home.html", http.StatusSeeOther) } diff --git a/static/home.html b/static/home.html new file mode 100644 index 0000000..4f3b067 --- /dev/null +++ b/static/home.html @@ -0,0 +1,50 @@ + + + + + + + HCA Tickets + + + + + + +
+ Profile +
+
{{.profile.name}}
+
{{.profile.email}}
+
+
+

Welcome to HCA Tickets!

+
+ + + diff --git a/static/index.html b/static/index.html index 8c8c09c..d45d51e 100644 --- a/static/index.html +++ b/static/index.html @@ -20,7 +20,7 @@