-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandle_code.go
48 lines (37 loc) · 948 Bytes
/
handle_code.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
package maildoor
import (
"context"
"net/http"
)
// handleCode validates the input handleCode with the passed email.
func (m *maildoor) handleCode(w http.ResponseWriter, r *http.Request) {
email := r.FormValue("email")
code := r.FormValue("code")
secret := secrets[email]
if code == "" {
renderError(m, w, email, "Code is required")
return
}
if !validateCode(code, secret) {
renderError(m, w, email, "Invalid code")
return
}
delete(secrets, email)
// Adding email to the context
r = r.WithContext(context.WithValue(r.Context(), "email", email))
m.afterLogin(w, r)
}
func renderError(m *maildoor, w http.ResponseWriter, email, errorMessage string) {
data := atempt{
Email: email,
Error: errorMessage,
Logo: m.logoURL,
Icon: m.iconURL,
ProductName: m.productName,
}
err := m.render(w, data, "layout.html", "handle_code.html")
if err != nil {
m.httpError(w, err)
return
}
}