-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandle_email.go
59 lines (49 loc) · 1.19 KB
/
handle_email.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
package maildoor
import (
"net/http"
)
// handleEmail endpoint validates the handleEmail and sends a token to the
// user by calling the handleEmail sender function.
func (m *maildoor) handleEmail(w http.ResponseWriter, r *http.Request) {
data := atempt{
Logo: m.logoURL,
ProductName: m.productName,
Icon: m.iconURL,
}
email := r.FormValue("email")
if err := m.emailValidator(email); err != nil {
data.Error = err.Error()
w.WriteHeader(http.StatusUnprocessableEntity)
err := m.render(w, data, "layout.html", "handle_login.html")
if err != nil {
m.httpError(w, err)
}
return
}
secret, err := generateSecret()
if err != nil {
m.httpError(w, err)
}
saveSecret(email, secret)
html, txt, err := m.mailBodies(generateCode(secret))
if err != nil {
m.httpError(w, err)
return
}
err = m.emailSender(email, html, txt)
if err != nil {
data.Error = err.Error()
w.WriteHeader(http.StatusInternalServerError)
err := m.render(w, data, "layout.html", "handle_login.html")
if err != nil {
m.httpError(w, err)
}
return
}
data.Email = email
err = m.render(w, data, "layout.html", "handle_code.html")
if err != nil {
m.httpError(w, err)
return
}
}