From 7b95d6b80dd2d1efb26f7c515383abd4f0dc9d42 Mon Sep 17 00:00:00 2001 From: Andinus Date: Sat, 28 Mar 2020 19:25:53 +0530 Subject: Prepare for rewrite on several functions User struct now exports everything, encapsulation is not necessary over here. Instead of introducing a new variable uInfo we'll use user.User and pass that. Handlers & related functions will be rewritten to work with this change. This will make it easier to work on later as the program grows. I'm also rethinking error handling. --- handler/web/login.go | 83 ------------------------------------------------- handler/web/page.go | 7 +++-- handler/web/register.go | 74 ------------------------------------------- 3 files changed, 5 insertions(+), 159 deletions(-) delete mode 100644 handler/web/login.go delete mode 100644 handler/web/register.go (limited to 'handler') diff --git a/handler/web/login.go b/handler/web/login.go deleted file mode 100644 index 0c70b56..0000000 --- a/handler/web/login.go +++ /dev/null @@ -1,83 +0,0 @@ -package web - -import ( - "fmt" - "html/template" - "log" - "net/http" - "time" - - "tildegit.org/andinus/perseus/auth" - "tildegit.org/andinus/perseus/auth/token" - "tildegit.org/andinus/perseus/core" - "tildegit.org/andinus/perseus/storage/sqlite3" -) - -// HandleLogin handles /login pages. -func HandleLogin(w http.ResponseWriter, r *http.Request, db *sqlite3.DB) { - p := Page{Version: core.Version()} - error := []string{} - success := []string{} - - switch r.Method { - case http.MethodGet: - t, _ := template.ParseFiles("web/login.html") - t.Execute(w, p) - - case http.MethodPost: - if err := r.ParseForm(); err != nil { - log.Printf("web/login.go: 400 Bad Request :: %s", err.Error()) - http.Error(w, "400 Bad Request", http.StatusBadRequest) - return - } - - // Get form values - uInfo := make(map[string]string) - uInfo["username"] = r.FormValue("username") - uInfo["password"] = r.FormValue("password") - - // Perform authentication - err := auth.Login(db, uInfo) - - if err != nil { - log.Printf("web/login.go: %s :: %s :: %s", - "login failed", - uInfo["username"], - err.Error()) - - error = append(error, - fmt.Sprintf("Login failed")) - - p.Error = error - } else { - success = append(success, - fmt.Sprintf("Login successful")) - p.Success = success - - // Set token if login was successful. - token, err := token.AddToken(db, uInfo) - if err != nil { - log.Printf("web/login.go: %s :: %s :: %s", - "token generation failed", - uInfo["username"], - err.Error()) - - error = append(error, - fmt.Sprintf("Token generation failed")) - } - // If token was generated then ask browser to - // set it as cookie. - expiration := time.Now().Add(1 * 24 * time.Hour) - cookie := http.Cookie{Name: "token", Value: token, Expires: expiration} - http.SetCookie(w, &cookie) - } - - t, _ := template.ParseFiles("web/login.html") - t.Execute(w, p) - - default: - w.WriteHeader(http.StatusMethodNotAllowed) - log.Printf("web/login.go: %v not allowed on %v", r.Method, r.URL) - } - -} diff --git a/handler/web/page.go b/handler/web/page.go index 647984d..1f457de 100644 --- a/handler/web/page.go +++ b/handler/web/page.go @@ -1,8 +1,11 @@ package web -import "html/template" +import ( + "html/template" +) -// Page holds page information +// Page holds page information that is sent to all webpages rendered +// by perseus. type Page struct { SafeList []template.HTML List []string diff --git a/handler/web/register.go b/handler/web/register.go deleted file mode 100644 index 1e76af2..0000000 --- a/handler/web/register.go +++ /dev/null @@ -1,74 +0,0 @@ -package web - -import ( - "fmt" - "html/template" - "log" - "net/http" - "strings" - - "tildegit.org/andinus/perseus/auth" - "tildegit.org/andinus/perseus/core" - "tildegit.org/andinus/perseus/storage/sqlite3" -) - -// HandleRegister handles /register pages. -func HandleRegister(w http.ResponseWriter, r *http.Request, db *sqlite3.DB) { - p := Page{Version: core.Version()} - p.Notice = []string{ - "Only [a-z] & [0-9] allowed for username", - "Password length must be greater than 8 characters", - } - switch r.Method { - case http.MethodGet: - t, _ := template.ParseFiles("web/register.html") - t.Execute(w, p) - - case http.MethodPost: - if err := r.ParseForm(); err != nil { - log.Printf("web/register.go: 400 Bad Request :: %s", err.Error()) - http.Error(w, "400 Bad Request", http.StatusBadRequest) - return - } - - // Get form values - uInfo := make(map[string]string) - uInfo["username"] = r.FormValue("username") - uInfo["password"] = r.FormValue("password") - - // Perform registration - err := auth.Register(db, uInfo) - - if err != nil { - log.Printf("web/register.go: %s :: %s :: %s", - "registration failed", - uInfo["username"], - err.Error()) - - error := []string{} - error = append(error, - fmt.Sprintf("Registration failed")) - - // Check if the error was because of username - // not being unique. - if strings.HasPrefix(err.Error(), "UNIQUE constraint failed") { - error = append(error, - fmt.Sprintf("Username not unique")) - } - p.Error = error - } else { - success := []string{} - success = append(success, - fmt.Sprintf("Registration successful")) - p.Success = success - } - - t, _ := template.ParseFiles("web/register.html") - t.Execute(w, p) - - default: - w.WriteHeader(http.StatusMethodNotAllowed) - log.Printf("web/register.go: %v not allowed on %v", r.Method, r.URL) - } - -} -- cgit 1.4.1-2-gfad0