/
register.go
84 lines (70 loc) · 2.14 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
// Package register handles the user creation.
package register
import (
"errors"
"net/http"
"github.com/blue-jay/blueprint/lib/flight"
"github.com/blue-jay/blueprint/middleware/acl"
"github.com/blue-jay/blueprint/model/user"
"github.com/blue-jay/core/form"
"github.com/blue-jay/core/passhash"
"github.com/blue-jay/core/router"
)
// Load the routes.
func Load() {
router.Get("/register", Index, acl.DisallowAuth)
router.Post("/register", Store, acl.DisallowAuth)
}
// Index displays the register page.
func Index(w http.ResponseWriter, r *http.Request) {
c := flight.Context(w, r)
v := c.View.New("register/index")
form.Repopulate(r.Form, v.Vars, "first_name", "last_name", "email")
v.Render(w, r)
}
// Store handles the registration form submission.
func Store(w http.ResponseWriter, r *http.Request) {
c := flight.Context(w, r)
// Validate with required fields
if !c.FormValid("first_name", "last_name", "email", "password", "password_verify") {
Index(w, r)
return
}
// Get form values
firstName := r.FormValue("first_name")
lastName := r.FormValue("last_name")
email := r.FormValue("email")
// Validate passwords
if r.FormValue("password") != r.FormValue("password_verify") {
c.FlashError(errors.New("Passwords do not match."))
Index(w, r)
return
}
// Hash password
password, errp := passhash.HashString(r.FormValue("password"))
// If password hashing failed
if errp != nil {
c.FlashErrorGeneric(errp)
http.Redirect(w, r, "/register", http.StatusFound)
return
}
// Get database result
_, noRows, err := user.ByEmail(c.DB, email)
if noRows { // If success (no user exists with that email)
_, err = user.Create(c.DB, firstName, lastName, email, password)
// Will only error if there is a problem with the query
if err != nil {
c.FlashErrorGeneric(err)
} else {
c.FlashSuccess("Account created successfully for: " + email)
http.Redirect(w, r, "/login", http.StatusFound)
return
}
} else if err != nil { // Catch all other errors
c.FlashErrorGeneric(err)
} else { // Else the user already exists
c.FlashError(errors.New("Account already exists for: " + email))
}
// Display the page
Index(w, r)
}