-
Notifications
You must be signed in to change notification settings - Fork 1
/
users.go
107 lines (99 loc) · 2.92 KB
/
users.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
package users
import (
"net/http"
"github.com/gorilla/sessions"
"github.com/livebud/bud/framework/controller/controllerrt/request"
pogo "github.com/livebud/weblog/bud/pkg/table"
"github.com/livebud/weblog/bud/pkg/table/user"
"github.com/livebud/weblog/middleware/csrf"
"github.com/livebud/weblog/view/users"
"golang.org/x/crypto/bcrypt"
"gopkg.in/go-playground/validator.v9"
)
func New(db pogo.DB, model *user.Model, view *users.View, sessions sessions.Store) *Controller {
return &Controller{db, model, view, sessions}
}
type Controller struct {
db pogo.DB
model *user.Model
view *users.View
sessions sessions.Store
}
func (c *Controller) Index(w http.ResponseWriter, r *http.Request) {
session, err := c.sessions.Get(r, "user")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Missing
userId, ok := session.Values["user_id"]
if !ok {
http.Redirect(w, r, "/login", http.StatusFound)
return
}
// Invalid type
userIdInt, ok := userId.(int)
if !ok {
http.Error(w, "invalid user id", http.StatusInternalServerError)
return
}
usr, err := c.model.FindByID(c.db, userIdInt)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html")
if err := c.view.Index.Render(w, &users.IndexProps{
CSRF: csrf.Token(r),
Name: usr.Name,
Email: usr.Email,
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (c *Controller) Signup(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
if err := c.view.Signup.Render(w, &users.Signup{
CSRF: csrf.Token(r),
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
var valid = validator.New()
func (c *Controller) Create(w http.ResponseWriter, r *http.Request) {
var input struct {
Name string `json:"name" validate:"required"`
Email string `json:"email" validate:"required"`
Password string `json:"password" validate:"required"`
}
if err := request.Unmarshal(r, &input); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := valid.Struct(input); err != nil {
// TODO: show form errors
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
password, err := bcrypt.GenerateFromPassword([]byte(input.Password), bcrypt.DefaultCost)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
usr, err := c.model.Insert(c.db, user.New().Name(input.Name).Email(input.Email).Password(string(password)))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Save the session
session, err := c.sessions.Get(r, "user")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
session.Values["user_id"] = usr.ID
// Redirect to the user's profile
http.Redirect(w, r, "/", http.StatusFound)
}