-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.go
116 lines (102 loc) · 2.74 KB
/
register.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"database/sql"
"fmt"
"html/template"
"net/http"
"regexp"
)
func register(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
buf, err := pages.ReadFile("pages/register.html")
if err != nil {
logError(err)
giveError(w, err)
}
var body string = string(buf)
tmpl, err := template.New("register").Parse(body)
if err != nil {
logError(err)
giveError(w, err)
return
}
if err := tmpl.Execute(w, nil); err != nil {
logError(err)
giveError(w, err)
return
}
case http.MethodPost:
response := struct {
Message string `json:"message"`
}{}
buf, err := pages.ReadFile("pages/register.html")
if err != nil {
logError(err)
giveError(w, err)
}
var body string = string(buf)
tmpl, err := template.New("register").Parse(body)
if err != nil {
logError(err)
giveError(w, err)
return
}
if err := r.ParseForm(); err != nil {
logError(err)
giveError(w, err)
return
}
match, err := regexp.MatchString(usernameRegexp, r.FormValue("username"))
if err != nil {
logError(err)
giveError(w, err)
return
}
if valid := func() bool {
if len(r.FormValue("username")) < minimumUsernameLength || len(r.FormValue("username")) > maximumUsernameLength || !match {
response.Message = fmt.Sprintf("Username minimum length: %s, Maximum length: %s\n, Regexp: %s", fmt.Sprint(minimumUsernameLength), fmt.Sprint(maximumUsernameLength), usernameRegexp)
return false
}
if len(r.FormValue("password")) < minimumPasswordLength || len(r.FormValue("password")) > maximumPasswordLength {
response.Message = fmt.Sprintf("Password minimum length: %s, Maximum length: %s\n", fmt.Sprint(minimumPasswordLength), fmt.Sprint(maximumPasswordLength))
return false
}
if r.FormValue("password") != r.FormValue("confirmPassword") {
response.Message = "Passwords do not match."
return false
}
_, err := idFromUsername(r.FormValue("username"))
if err != sql.ErrNoRows {
response.Message = "That username already exists."
return false
}
// if !verifyRecaptcha(r.FormValue("g-recaptcha-response"), recaptchaSecret) {
// response.Message = "reCAPTCHA verification failed."
// return false
// }
return true
}(); valid {
_, token, err := createUser(r.FormValue("username"), r.FormValue("password"), joinCash, false, db)
if err != nil {
logError(err)
giveError(w, err)
return
}
http.SetCookie(w, &http.Cookie{
Name: "token",
Value: token,
})
http.Redirect(w, r, "/home", http.StatusSeeOther)
return
}
if err := tmpl.Execute(w, response); err != nil {
logError(err)
giveError(w, err)
return
}
default:
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
}