summary refs log tree commit diff stats
path: root/handler
diff options
context:
space:
mode:
Diffstat (limited to 'handler')
-rw-r--r--handler/web/login.go83
-rw-r--r--handler/web/page.go7
-rw-r--r--handler/web/register.go74
3 files changed, 5 insertions, 159 deletions
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)
-	}
-
-}