From c0b826afa81201d336370b1e4a971b2dc5e2a3bb Mon Sep 17 00:00:00 2001 From: Logan Date: Fri, 27 Sep 2024 14:37:49 -0500 Subject: [PATCH] Fixed bug with session handling --- db.json | 1 + go.mod | 5 +- go.sum | 2 + server.go | 27 ++++--- static/index.html | 7 +- static/login.html | 6 +- static/register.html | 12 ++-- users.go | 164 ++++++++++++++++++++++++++----------------- users.json | 3 - 9 files changed, 132 insertions(+), 95 deletions(-) create mode 100644 db.json delete mode 100644 users.json diff --git a/db.json b/db.json new file mode 100644 index 0000000..f7fe8a3 --- /dev/null +++ b/db.json @@ -0,0 +1 @@ +{"xterminate18181@gmail.com":{"email":"xterminate18181@gmail.com","password":"123","account-created":"2024-09-27T05:39:11.331520582-05:00","last-login":"2024-09-27T05:39:11.331520621-05:00"}} diff --git a/go.mod b/go.mod index 3494795..d3e4ddd 100644 --- a/go.mod +++ b/go.mod @@ -4,4 +4,7 @@ go 1.23 require github.com/gorilla/sessions v1.4.0 -require github.com/gorilla/securecookie v1.1.2 // indirect +require ( + github.com/gorilla/mux v1.8.1 // indirect + github.com/gorilla/securecookie v1.1.2 // indirect +) diff --git a/go.sum b/go.sum index fe47730..7e011b0 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA= github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo= github.com/gorilla/sessions v1.4.0 h1:kpIYOp/oi6MG/p5PgxApU8srsSw9tuFbt46Lt7auzqQ= diff --git a/server.go b/server.go index b75530b..734938b 100644 --- a/server.go +++ b/server.go @@ -1,10 +1,10 @@ package main import ( - "crypto/rand" "fmt" "log" "net/http" + "strings" "github.com/gorilla/sessions" ) @@ -18,17 +18,8 @@ type Server struct { } func main() { - // Set up encryption for session tokens - fmt.Print("Generating encryption key... ") - secret := make([]byte, 32) - _, err := rand.Read(secret) - if err != nil { - fmt.Println("Error generating key:") - log.Fatal(err) - return - } - fmt.Println("Done!") // Create server object + secret := []byte("super-secret-key") server := Server{ Users: make(map[string]UserData), Sessions: sessions.NewCookieStore(secret), @@ -36,10 +27,16 @@ func main() { // Host static files static_files := http.FileServer(http.Dir("static/")) http.Handle("/", static_files) - // Response generated by code - http.HandleFunc("/handle-register", server.handle_register) - http.HandleFunc("/handle-login", server.handle_login) - + // Redirect .html to clean URL + http.Handle("/register.html", http.RedirectHandler("/register", 301)) + http.Handle("/login.html", http.RedirectHandler("/login", 301)) + // Handle user authentication + http.HandleFunc("/register", server.handle_register) + http.HandleFunc("/login", server.handle_login) + http.HandleFunc("/logout", func(w http.ResponseWriter, r *http.Request) { + server.handle_logout(w, r) + http.Redirect(w, r, "/", http.StatusFound) + }) // Start web server at 127.0.0.1:8080 fmt.Printf("Listening to %s on port %s...\n", ADDRESS, PORT) e := http.ListenAndServe(ADDRESS+":"+PORT, nil) diff --git a/static/index.html b/static/index.html index 823624b..af83bfb 100644 --- a/static/index.html +++ b/static/index.html @@ -14,11 +14,14 @@ - Register + Register - Login + Login + + + Log Out