aboutsummaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
Diffstat (limited to 'api')
-rw-r--r--api/routes.go15
-rw-r--r--api/user.go59
2 files changed, 54 insertions, 20 deletions
diff --git a/api/routes.go b/api/routes.go
index d0e8cec..404f821 100644
--- a/api/routes.go
+++ b/api/routes.go
@@ -48,6 +48,13 @@ var Groups = []Group{
48 {"GET", []gin.HandlerFunc{GetPortfolio}, "/:name/portfolio"}, 48 {"GET", []gin.HandlerFunc{GetPortfolio}, "/:name/portfolio"},
49 }, 49 },
50 }, 50 },
51 {
52 "/user",
53 []Middleware{JwtAuth, UserConfirmed, OtpAuth},
54 []Route{
55 {"GET", []gin.HandlerFunc{UserAccount}, "/account"},
56 },
57 },
51} 58}
52 59
53func Signup(c *gin.Context) { 60func Signup(c *gin.Context) {
@@ -169,3 +176,11 @@ func ConfirmEmail(c *gin.Context) {
169 176
170 RunQuery(query, c) 177 RunQuery(query, c)
171} 178}
179
180func UserAccount(c *gin.Context) {
181 query := &UserAccountQuery{}
182
183 query.In.User = GetUser(c)
184
185 RunQuery(query, c)
186}
diff --git a/api/user.go b/api/user.go
index 2848696..a2737fd 100644
--- a/api/user.go
+++ b/api/user.go
@@ -16,6 +16,26 @@ const (
16 VALID_EMAIL_REGEX = `(?i)^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$` 16 VALID_EMAIL_REGEX = `(?i)^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$`
17) 17)
18 18
19func UserConfirmed(c *gin.Context) *Error {
20 user, exists := c.Get("user")
21
22 if !exists {
23 return &Error{NotAuthorized, "not authorized", fmt.Errorf("no user key in context")}
24 }
25
26 if user.(db.User).Status != db.Confirmed {
27 return &Error{UserNotConfirmed, "user awaiting admin validation", fmt.Errorf("user '%v' not confirmed", user)}
28 }
29
30 return nil
31}
32
33func GetUser(c *gin.Context) db.User {
34 user, _ := c.Get("user")
35
36 return user.(db.User)
37}
38
19func IsValidEmailAddress(email string) bool { 39func IsValidEmailAddress(email string) bool {
20 r := regexp.MustCompile(VALID_EMAIL_REGEX) 40 r := regexp.MustCompile(VALID_EMAIL_REGEX)
21 41
@@ -142,26 +162,6 @@ func (q SigninQuery) Run() (interface{}, *Error) {
142 return SignResult{token}, nil 162 return SignResult{token}, nil
143} 163}
144 164
145func UserConfirmed(c *gin.Context) *Error {
146 user, exists := c.Get("user")
147
148 if !exists {
149 return &Error{NotAuthorized, "not authorized", fmt.Errorf("no user key in context")}
150 }
151
152 if user.(db.User).Status != db.Confirmed {
153 return &Error{UserNotConfirmed, "user awaiting admin validation", fmt.Errorf("user '%v' not confirmed", user)}
154 }
155
156 return nil
157}
158
159func GetUser(c *gin.Context) db.User {
160 user, _ := c.Get("user")
161
162 return user.(db.User)
163}
164
165type ConfirmEmailQuery struct { 165type ConfirmEmailQuery struct {
166 In struct { 166 In struct {
167 Token string 167 Token string
@@ -214,3 +214,22 @@ func (q ConfirmEmailQuery) Run() (interface{}, *Error) {
214 214
215 return nil, nil 215 return nil, nil
216} 216}
217
218type UserAccountQuery struct {
219 In struct {
220 User db.User
221 }
222 Out struct {
223 Email string `json:"email"`
224 }
225}
226
227func (q UserAccountQuery) ValidateParams() *Error {
228 return nil
229}
230
231func (q UserAccountQuery) Run() (interface{}, *Error) {
232 q.Out.Email = q.In.User.Email
233
234 return q.Out, nil
235}