From 3462fc966cbddd60a2d5520d374149df360e0367 Mon Sep 17 00:00:00 2001 From: Logan Date: Wed, 23 Oct 2024 16:44:49 -0500 Subject: [PATCH] Prototype for backend email confirmaion --- server.go | 21 +-------------------- static/register.html | 4 ++-- users.go | 16 +++++++++++++++- 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/server.go b/server.go index 95c38c6..d9f2c3b 100644 --- a/server.go +++ b/server.go @@ -1,7 +1,6 @@ package main import ( - "encoding/json" "flag" "fmt" "log" @@ -13,22 +12,11 @@ import ( const CANVAS_WIDTH = 1000 const CANVAS_HEIGHT = 1000 -type Canvas struct { - Data [CANVAS_WIDTH * CANVAS_HEIGHT]byte `json:"data"` - Width int `json:"width"` - Height int `json:"height"` -} - type Server struct { // Registered user information Users map[string]UserData // Login sessions Sessions *sessions.CookieStore - Canvas Canvas -} - -func (s *Server) canvas(w http.ResponseWriter, r *http.Request) { - json.NewEncoder(w).Encode(s.canvas) } func main() { @@ -43,13 +31,6 @@ func main() { server := Server{ Users: make(map[string]UserData), Sessions: sessions.NewCookieStore(secret), - Canvas: Canvas{}, - } - - for x := 0; x < CANVAS_WIDTH; x++ { - for y := 0; y < CANVAS_HEIGHT; y++ { - server.Canvas.Data[y*CANVAS_HEIGHT+x] = byte(CANVAS_WIDTH / x) - } } // Host static files @@ -66,7 +47,7 @@ func main() { http.Redirect(w, r, "/", http.StatusFound) }) http.HandleFunc("/secret", server.secret) - http.HandleFunc("/canvas", canvas) + http.HandleFunc("/confirm-email", server.handle_confirmation) // Start web server at 127.0.0.1:8080 fmt.Printf("Listening to %s on port %s...\n", address, port) err := http.ListenAndServe(address+":"+port, nil) diff --git a/static/register.html b/static/register.html index 2dd9030..2a566b3 100644 --- a/static/register.html +++ b/static/register.html @@ -12,7 +12,7 @@

Registration

-
+
@@ -35,7 +35,7 @@

- +
diff --git a/users.go b/users.go index 450be15..d9de3e6 100644 --- a/users.go +++ b/users.go @@ -13,6 +13,7 @@ import ( const SESSION_COOKIE_NAME = "utsa-place-session" const SESSION_AUTH = "auth" const SESSION_STARTED = "age" +const SESSION_CONFIRMED = "confirmed" const ENCRYPTION_STRENGTH = 14 @@ -126,16 +127,29 @@ func (s *Server) handle_register(w http.ResponseWriter, r *http.Request) { // Make session valid session.Values[SESSION_AUTH] = true session.Values[SESSION_STARTED] = now.String() + session.Values[SESSION_CONFIRMED] = false // Send session token to browser session.Save(r, w) // Redirect to index.html fmt.Println("Registered user: ", email) - http.Redirect(w, r, "/", http.StatusFound) + http.Redirect(w, r, "/confirm-email", http.StatusFound) default: http.Error(w, "Forbidden", http.StatusForbidden) } } +func (s *Server) handle_confirmation(w http.ResponseWriter, r *http.Request) { + session, err := s.Sessions.Get(r, SESSION_COOKIE_NAME) + if err != nil { + s.handle_logout(w, r) + http.Redirect(w, r, "/register", http.StatusFound) + return + } + confirmed := session.Values[SESSION_CONFIRMED] + fmt.Println("Session confirmed: ", confirmed) + http.ServeFile(w, r, "./static/confirmation.html") +} + func (s *Server) secret(w http.ResponseWriter, r *http.Request) { session, _ := s.Sessions.Get(r, SESSION_COOKIE_NAME)