-
Notifications
You must be signed in to change notification settings - Fork 84
/
users-api.go
252 lines (208 loc) · 5.18 KB
/
users-api.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package main
import (
"log"
"net/http"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/pkg/errors"
)
func usersAPI(r chi.Router) {
r.Group(func(r chi.Router) {
r.Route("/",
func(r chi.Router) {
r.Post("/signup", UsersSignup)
r.Post("/logout", UsersLogout)
r.Post("/login", UsersLogin)
r.Post("/verify", VerifyEmail)
r.Route("/verify/{KEY}", func(r chi.Router) {
r.Post("/", VerifyEmail)
})
r.Route("/reset/{EMAIL}", func(r chi.Router) {
r.Post("/", ResetEmail)
})
r.Route("/setpassword", func(r chi.Router) {
r.Post("/", SetPassword)
})
r.Route("/invite/{CODE}", func(r chi.Router) {
r.Get("/", getInvite)
r.Post("/", acceptInvite)
})
})
})
}
// UsersLogin ...
func UsersLogin(w http.ResponseWriter, r *http.Request) {
data := &LoginRequest{}
if err := render.Bind(r, data); err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
s := GetEnv(r).Service
// Check email and password
acc, err := s.Login(data.Email, data.Password)
if err != nil {
_ = render.Render(w, r, ErrInvalidRequest(errors.New("email or password is incorrect")))
return
}
// Return token
token := s.Token(acc.ID)
addCookie(w, "jwt", token, s.GetConfig().Environment)
type response struct {
Token string `json:"token"`
}
render.JSON(w, r, &response{Token: token})
}
// UsersLogout ...
func UsersLogout(w http.ResponseWriter, r *http.Request) {
deleteCookie(w, "jwt")
render.Status(r, http.StatusOK)
}
// UsersSignup ...
func UsersSignup(w http.ResponseWriter, r *http.Request) {
data := &SignupRequest{}
if err := render.Bind(r, data); err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
s := GetEnv(r).Service
_, acc, _, err := s.Register(data.WorkspaceName, data.Name, data.Email, data.Password)
if err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
token := s.Token(acc.ID)
addCookie(w, "jwt", token, s.GetConfig().Environment)
render.Status(r, http.StatusOK)
_ = render.Render(w, r, &TokenResponse{Token: token})
}
// LoginRequest ...
type LoginRequest struct {
Email string `json:"email"`
Password string `json:"password"`
}
// Bind ...
func (p *LoginRequest) Bind(r *http.Request) error {
return nil
}
// SignupRequest ...
type SignupRequest struct {
WorkspaceName string `json:"workspaceName"`
Name string `json:"name"`
Email string `json:"email"`
Password string `json:"password"`
}
// Bind ...
func (p *SignupRequest) Bind(r *http.Request) error {
return nil
}
// TokenResponse ...
type TokenResponse struct {
Token string `json:"token"`
}
// Render ...
func (rd *TokenResponse) Render(w http.ResponseWriter, r *http.Request) error {
return nil
}
func addCookie(w http.ResponseWriter, name string, value string, env string) {
expire := time.Now().UTC().AddDate(10, 0, 0)
cookie := http.Cookie{
Name: name,
Value: value,
Expires: expire,
Path: "/",
HttpOnly: true,
Secure: true,
}
if env == "development" {
cookie.Secure = false
}
http.SetCookie(w, &cookie)
}
func deleteCookie(w http.ResponseWriter, name string) {
cookie := http.Cookie{
Name: name,
MaxAge: -1,
Path: "/",
}
http.SetCookie(w, &cookie)
}
// VerifyEmail ...
func VerifyEmail(w http.ResponseWriter, r *http.Request) {
key := chi.URLParam(r, "KEY")
s := GetEnv(r).Service
err := s.ConfirmEmail(key)
if err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
return
}
// ResetEmail ...
func ResetEmail(w http.ResponseWriter, r *http.Request) {
email := chi.URLParam(r, "EMAIL")
s := GetEnv(r).Service
err := s.SendResetEmail(email)
if err != nil {
log.Println("error sending mail")
}
return
}
// SetPasswordRequest ...
type SetPasswordRequest struct {
Key string `json:"key"`
Password string `json:"password"`
}
// Bind ...
func (p *SetPasswordRequest) Bind(r *http.Request) error {
return nil
}
// SetPassword ...
func SetPassword(w http.ResponseWriter, r *http.Request) {
data := &SetPasswordRequest{}
if err := render.Bind(r, data); err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
err := GetEnv(r).Service.SetPassword(data.Password, data.Key)
if err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
return
}
func getInvite(w http.ResponseWriter, r *http.Request) {
code := chi.URLParam(r, "CODE")
invite, err := GetEnv(r).Service.GetInvite(code)
if err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
render.JSON(w, r, invite)
}
func acceptInvite(w http.ResponseWriter, r *http.Request) {
code := chi.URLParam(r, "CODE")
err := GetEnv(r).Service.AcceptInvite(code)
if err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
return
}
// ContactRequest ...
type ContactRequest struct {
Topic string `json:"topic"`
Body string `json:"body"`
Sender string `json:"sender"`
}
// Bind ...
func (p *ContactRequest) Bind(r *http.Request) error {
return nil
}
type changeGeneralInfoRequest struct {
EUVAT string `json:"euVat"`
ExternalBillingEmail string `json:"externalBillingEmail"`
}
func (p *changeGeneralInfoRequest) Bind(r *http.Request) error {
return nil
}