Add post-auth landing page with cookie-based profile storage
This commit is contained in:
48
main.go
48
main.go
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
@@ -35,6 +36,11 @@ func handleStatic(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
name := strings.TrimPrefix(path, "/")
|
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") {
|
if strings.HasSuffix(name, ".html") {
|
||||||
t := templates.Lookup(name)
|
t := templates.Lookup(name)
|
||||||
if t == nil {
|
if t == nil {
|
||||||
@@ -42,13 +48,20 @@ func handleStatic(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "text/html")
|
w.Header().Set("Content-Type", "text/html")
|
||||||
t.Execute(w, envMap())
|
t.Execute(w, templateData(r))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
http.ServeFile(w, r, filepath.Join("static", name))
|
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 {
|
func envMap() map[string]string {
|
||||||
m := map[string]string{}
|
m := map[string]string{}
|
||||||
for _, e := range os.Environ() {
|
for _, e := range os.Environ() {
|
||||||
@@ -59,6 +72,33 @@ func envMap() map[string]string {
|
|||||||
return m
|
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) {
|
func handleGoogleCallback(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||||
@@ -78,12 +118,12 @@ func handleGoogleCallback(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
response := map[string]any{
|
profile := map[string]any{
|
||||||
"email": payload.Claims["email"],
|
"email": payload.Claims["email"],
|
||||||
"name": payload.Claims["name"],
|
"name": payload.Claims["name"],
|
||||||
"picture": payload.Claims["picture"],
|
"picture": payload.Claims["picture"],
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
setProfile(w, profile)
|
||||||
json.NewEncoder(w).Encode(response)
|
http.Redirect(w, r, "/home.html", http.StatusSeeOther)
|
||||||
}
|
}
|
||||||
|
|||||||
50
static/home.html
Normal file
50
static/home.html
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<script>document.documentElement.className = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'wa-dark' : 'wa-light';</script>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>HCA Tickets</title>
|
||||||
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@awesome.me/webawesome@3/dist-cdn/styles/themes/default.css">
|
||||||
|
<script type="module" src="https://cdn.jsdelivr.net/npm/@awesome.me/webawesome@3/dist-cdn/webawesome.loader.js"></script>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: var(--wa-font-sans);
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
min-height: 100vh;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
.profile {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 1rem;
|
||||||
|
}
|
||||||
|
.profile img {
|
||||||
|
width: 64px;
|
||||||
|
height: 64px;
|
||||||
|
border-radius: 50%;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body style="opacity: 0">
|
||||||
|
<wa-card>
|
||||||
|
<div class="profile" slot="header">
|
||||||
|
<img src="{{.profile.picture}}" alt="Profile">
|
||||||
|
<div>
|
||||||
|
<div><strong>{{.profile.name}}</strong></div>
|
||||||
|
<div>{{.profile.email}}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<p>Welcome to HCA Tickets!</p>
|
||||||
|
</wa-card>
|
||||||
|
<script type="module">
|
||||||
|
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => {
|
||||||
|
document.documentElement.className = e.matches ? 'wa-dark' : 'wa-light';
|
||||||
|
});
|
||||||
|
await customElements.whenDefined('wa-card');
|
||||||
|
document.body.style.opacity = 1;
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
</head>
|
</head>
|
||||||
<body style="opacity: 0">
|
<body style="opacity: 0">
|
||||||
<div id="g_id_onload"
|
<div id="g_id_onload"
|
||||||
data-client_id="{{.GOOGLE_CLIENT_ID}}"
|
data-client_id="{{.env.GOOGLE_CLIENT_ID}}"
|
||||||
data-login_uri="/auth/google/callback"
|
data-login_uri="/auth/google/callback"
|
||||||
data-ux_mode="redirect"
|
data-ux_mode="redirect"
|
||||||
data-use_fedcm_for_button="true"
|
data-use_fedcm_for_button="true"
|
||||||
|
|||||||
Reference in New Issue
Block a user