summary refs log tree commit diff stats
path: root/handler/web/login.go
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2020-03-28 19:25:53 +0530
committerAndinus <andinus@nand.sh>2020-03-28 19:25:53 +0530
commit7b95d6b80dd2d1efb26f7c515383abd4f0dc9d42 (patch)
tree76dca236dc2d7c3be989896442cb3edfa1235e8d /handler/web/login.go
parentb0b83af1c45c2d5ee587dc96847e25c95a3d50b0 (diff)
downloadperseus-7b95d6b80dd2d1efb26f7c515383abd4f0dc9d42.tar.gz
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.
Diffstat (limited to 'handler/web/login.go')
-rw-r--r--handler/web/login.go83
1 files changed, 0 insertions, 83 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)
-	}
-
-}