1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
package web
import (
"fmt"
"html/template"
"log"
"net/http"
"strings"
"tildegit.org/andinus/perseus/auth"
"tildegit.org/andinus/perseus/storage/sqlite3"
)
// HandleRegister handles /register pages.
func HandleRegister(w http.ResponseWriter, r *http.Request, db *sqlite3.DB) {
p := Page{}
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)
}
}
|