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 controllers
import (
"net/http"
"gitrepo.ru/neonxp/gorum/models"
"gitrepo.ru/neonxp/gorum/views"
"go.neonxp.ru/mux"
"go.neonxp.ru/mux/middleware/session"
)
func (rt *Router) Login(w http.ResponseWriter, r *http.Request) {
req := new(loginRequest)
if r.Method == http.MethodPost {
if err := mux.Bind(r, req); err != nil {
mux.Render(w, r, mux.DefaultErrorHandler(err))
return
}
u, err := rt.userRepo.Login(req.Email, req.Password)
if err != nil {
mux.Render(w, r, mux.DefaultErrorHandler(err))
return
}
sess := session.FromRequest(r)
(*sess)["user"] = u
mux.Redirect(w, 302, "/")
return
}
mux.Render(w, r, views.Login(req.Email))
}
func (rt *Router) Logout(w http.ResponseWriter, r *http.Request) {
session.Clear(w, r)
mux.Redirect(w, 302, "/")
}
func (rt *Router) Register(w http.ResponseWriter, r *http.Request) {
req := new(registerRequest)
if err := mux.Bind(r, req); err != nil {
mux.Render(w, r, mux.DefaultErrorHandler(err))
return
}
if r.Method == http.MethodPost {
if err := rt.userRepo.Create(req.Email, req.Password, req.Username, models.RoleUser); err != nil {
mux.Render(w, r, mux.DefaultErrorHandler(err))
return
}
mux.Redirect(w, 302, "/login")
return
}
mux.Render(w, r, views.Register(req.Username, req.Email))
}
|