-
Notifications
You must be signed in to change notification settings - Fork 214
/
userman.go
156 lines (127 loc) · 4.37 KB
/
userman.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
package handlers
import (
"context"
"encoding/json"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/spaceuptech/space-cloud/modules/userman"
"github.com/spaceuptech/space-cloud/utils"
)
// HandleProfile returns the handler for fetching single user profile
func HandleProfile(userManagement *userman.Module) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Create a context of execution
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
defer r.Body.Close()
// Get the path parameters
vars := mux.Vars(r)
project := vars["project"]
dbType := vars["dbType"]
id := vars["id"]
// Get the JWT token from header
token := utils.GetTokenFromHeader(r)
status, result, err := userManagement.Profile(ctx, token, dbType, project, id)
w.WriteHeader(status)
if err != nil {
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}
json.NewEncoder(w).Encode(map[string]interface{}{"user": result})
}
}
// HandleProfiles returns the handler for fetching all user profiles
func HandleProfiles(userManagement *userman.Module) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Create a context of execution
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
// Get the path parameters
vars := mux.Vars(r)
project := vars["project"]
dbType := vars["dbType"]
// Get the JWT token from header
token := utils.GetTokenFromHeader(r)
defer r.Body.Close()
status, result, err := userManagement.Profiles(ctx, token, dbType, project)
w.WriteHeader(status)
if err != nil {
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}
json.NewEncoder(w).Encode(result)
}
}
// HandleEmailSignIn returns the handler for email sign in
func HandleEmailSignIn(userManagement *userman.Module) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Create a context of execution
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
// Get the path parameters
vars := mux.Vars(r)
project := vars["project"]
dbType := vars["dbType"]
// Load the request from the body
req := map[string]interface{}{}
json.NewDecoder(r.Body).Decode(&req)
defer r.Body.Close()
status, result, err := userManagement.EmailSignIn(ctx, dbType, project, req["email"].(string), req["pass"].(string))
w.WriteHeader(status)
if err != nil {
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}
json.NewEncoder(w).Encode(result)
}
}
// HandleEmailSignUp returns the handler for email sign up
func HandleEmailSignUp(userManagement *userman.Module) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Create a context of execution
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
// Get the path parameters
vars := mux.Vars(r)
project := vars["project"]
dbType := vars["dbType"]
// Load the request from the body
req := map[string]interface{}{}
json.NewDecoder(r.Body).Decode(&req)
defer r.Body.Close()
status, result, err := userManagement.EmailSignUp(ctx, dbType, project, req["email"].(string), req["name"].(string), req["pass"].(string), req["role"].(string))
w.WriteHeader(status)
if err != nil {
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}
json.NewEncoder(w).Encode(result)
}
}
// HandleEmailEditProfile returns the handler for edit profile
func HandleEmailEditProfile(userManagement *userman.Module) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Create a context of execution
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
// Get the path parameters
vars := mux.Vars(r)
project := vars["project"]
dbType := vars["dbType"]
id := vars["id"]
// Get the JWT token from header
token := utils.GetTokenFromHeader(r)
// Load the request from the body
req := map[string]interface{}{}
json.NewDecoder(r.Body).Decode(&req)
defer r.Body.Close()
status, result, err := userManagement.EmailEditProfile(ctx, token, dbType, project, id, req["email"].(string), req["name"].(string), req["pass"].(string))
w.WriteHeader(status)
if err != nil {
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}
json.NewEncoder(w).Encode(result)
}
}