This repository has been archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
user.go
204 lines (173 loc) · 4.83 KB
/
user.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
package gosaas
import (
"fmt"
"math/rand"
"net/http"
"github.com/dstpierre/gosaas/data"
"github.com/dstpierre/gosaas/internal/config"
"github.com/dstpierre/gosaas/model"
"golang.org/x/crypto/bcrypt"
)
// User handles everything related to the /user requests
type User struct{}
func newUser() *Route {
var u interface{} = User{}
return &Route{
AllowCrossOrigin: true,
Logger: true,
MinimumRole: model.RolePublic,
WithDB: true,
Handler: u.(http.Handler),
}
}
func (u User) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var head string
head, r.URL.Path = ShiftPath(r.URL.Path)
if head == "signup" {
if r.Method == http.MethodGet {
u.signup(w, r)
} else if r.Method == http.MethodPost {
u.create(w, r)
}
} else if head == "login" {
if r.Method == http.MethodGet {
u.login(w, r)
} else if r.Method == http.MethodPost {
u.signin(w, r)
}
} else if head == "profile" && r.Method == http.MethodGet {
u.profile(w, r)
} else {
notFound(w)
}
}
func (u User) signup(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ServePage(w, r, config.Current.SignUpTemplate, CreateViewData(ctx, nil, nil))
}
func (u User) create(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
db := ctx.Value(ContextDatabase).(*data.DB)
isJSON := ctx.Value(ContextContentIsJSON).(bool)
var data = new(struct {
Email string `json:"email"`
Password string `json:"password"`
})
if isJSON {
if err := ParseBody(r.Body, &data); err != nil {
Respond(w, r, http.StatusBadRequest, err)
return
}
} else {
r.ParseForm()
data.Email = r.Form.Get("email")
}
if len(data.Password) == 0 {
data.Password = randStringRunes(7)
fmt.Println("TODO: remove this, temporary password", data.Password)
}
b, err := bcrypt.GenerateFromPassword([]byte(data.Password), bcrypt.DefaultCost)
if err != nil {
if isJSON {
Respond(w, r, http.StatusInternalServerError, err)
} else {
http.Redirect(w, r, config.Current.SignUpErrorRedirect, http.StatusSeeOther)
}
return
}
acct, err := db.Users.SignUp(data.Email, string(b))
if err != nil {
if isJSON {
Respond(w, r, http.StatusInternalServerError, err)
} else {
http.Redirect(w, r, config.Current.SignUpErrorRedirect, http.StatusSeeOther)
}
return
}
if config.Current.SignUpSendEmailValidation {
u.sendEmail(acct.Email, data.Password)
}
if isJSON {
Respond(w, r, http.StatusCreated, acct)
} else {
ck := &http.Cookie{
Name: "X-API-KEY",
Path: "/",
Value: acct.Users[0].Token,
}
// we set the cookie so they will have their authentication token on the next request.
http.SetCookie(w, ck)
http.Redirect(w, r, config.Current.SignUpSuccessRedirect, http.StatusSeeOther)
}
}
func (u User) sendEmail(email, pass string) {
//TODO: implement this
//queue.Enqueue(queue.TaskEmail, queue.SendEmailParameter{})
}
func (u User) login(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ServePage(w, r, config.Current.SignInTemplate, CreateViewData(ctx, nil, nil))
}
func (u User) signin(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
db := ctx.Value(ContextDatabase).(*data.DB)
isJSON := ctx.Value(ContextContentIsJSON).(bool)
var data = new(struct {
Email string `json:"email"`
Password string `json:"password"`
})
if isJSON {
if err := ParseBody(r.Body, &data); err != nil {
Respond(w, r, http.StatusBadRequest, err)
return
}
} else {
r.ParseForm()
data.Email = r.Form.Get("email")
data.Password = r.Form.Get("password")
}
user, err := db.Users.GetUserByEmail(data.Email)
if err != nil {
if isJSON {
Respond(w, r, http.StatusInternalServerError, err)
} else {
http.Redirect(w, r, config.Current.SignInErrorRedirect, http.StatusSeeOther)
}
return
}
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(data.Password)); err != nil {
Respond(w, r, http.StatusNotFound, fmt.Errorf("invalid email/password."))
return
}
if isJSON {
Respond(w, r, http.StatusOK, user)
} else {
ck := &http.Cookie{
Name: "X-API-KEY",
Path: "/",
Value: user.Token,
}
// we set the cookie so they will have their authentication token on the next request.
http.SetCookie(w, ck)
http.Redirect(w, r, config.Current.SignInSuccessRedirect, http.StatusSeeOther)
}
}
func (u User) profile(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
keys := ctx.Value(ContextAuth).(Auth)
db := ctx.Value(ContextDatabase).(*data.DB)
acct, err := db.Users.GetDetail(keys.AccountID)
if err != nil {
Respond(w, r, http.StatusInternalServerError, err)
return
}
Respond(w, r, http.StatusOK, acct)
}
func randStringRunes(n int) string {
letterRunes := []rune("abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ2345679")
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}