generated from golangsrilanka/go-puso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
223 lines (193 loc) · 5.75 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package handler
import (
b64 "encoding/base64"
"encoding/json"
"fmt"
"net/http"
"github.com/go-chi/chi"
"github.com/sithumonline/quick-note/config"
"github.com/sithumonline/quick-note/internal/auth"
"github.com/sithumonline/quick-note/internal/mail"
"github.com/sithumonline/quick-note/models"
"github.com/sithumonline/quick-note/transact/user"
)
type UserHandler struct {
repo user.UserRepo
auth auth.Auth
mail mail.Mail
}
func NewUserHandler(repo user.UserRepo, auth auth.Auth, mail mail.Mail) *UserHandler {
return &UserHandler{
repo: repo,
auth: auth,
mail: mail,
}
}
// Signup
// @Summary Create a new user
// @Tags User
// @Accept json
// @Produce json
// @Param data body models.User true "data"
// @Success 200 {string} string "successfully note created"
// @Failure 400 {string} string
// @Failure 500 {string} string
// @Router /user/signup [post]
func (p *UserHandler) Signup(w http.ResponseWriter, r *http.Request) {
t := models.User{}
if err := json.NewDecoder(r.Body).Decode(&t); err != nil {
RespondWithError(w, http.StatusBadRequest, err.Error())
return
}
if err := p.repo.Save(&t); err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}
token, err := p.repo.GetTokenWithoutCred(&models.Credentials{Email: t.Email}, false)
p.mail.Send(mail.MailBody{
ReceiversAddress: t.Email,
Subject: "[Quick Note] Email Verification",
Body: fmt.Sprintf(
"<a href=\"%vverify/%v/%v\" rel=\"noreferrer\" target=\"_blank\">Verify</a>",
config.GetEnv("frontend.url"),
token,
b64.StdEncoding.EncodeToString([]byte(t.Email)),
),
})
if err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}
RespondWithJSON(w, http.StatusOK, "successfully user created")
}
// Signin
// @Summary Sign in user
// @Tags User
// @Accept json
// @Produce json
// @Param data body models.User true "data"
// @Success 200 {string} string "successfully note created"
// @Failure 400 {string} string
// @Failure 500 {string} string
// @Router /user/signin [post]
func (p *UserHandler) Signin(w http.ResponseWriter, r *http.Request) {
t := models.Credentials{}
if err := json.NewDecoder(r.Body).Decode(&t); err != nil {
RespondWithError(w, http.StatusBadRequest, err.Error())
return
}
token, err := p.repo.GetTokenByCred(&t)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}
id, err := p.repo.GetIdByEmail(&t, true)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}
RespondWithJSON(w, http.StatusOK, token+";"+b64.StdEncoding.EncodeToString([]byte(id)))
}
// Welcome
// @Summary Check the token
// @Tags User
// @Accept json
// @Produce json
// @Success 200 {string} string
// @Failure 404 {string} string
// @Router /user/welcome [get]
func (p *UserHandler) Welcome(w http.ResponseWriter, r *http.Request) {
v, httpStatus, err := p.auth.TokenValidation(r, true)
if !v {
RespondWithError(w, httpStatus, err.Error())
return
}
RespondWithJSON(w, http.StatusOK, "Welcome to quick-note")
}
// Reset
// @Summary Re-set the user password
// @Tags User
// @Accept json
// @Produce json
// @Param userEmail path string true "email"
// @Success 200 {string} string
// @Failure 404 {string} string
// @Router /user/reset/{userEmail} [get]
func (p *UserHandler) Reset(w http.ResponseWriter, r *http.Request) {
bMail, _ := b64.StdEncoding.DecodeString(chi.URLParam(r, "userEmail"))
uMail := string(bMail)
id, err := p.repo.GetIdByEmail(&models.Credentials{Email: uMail}, true)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}
token, err := p.repo.GetTokenWithoutCred(&models.Credentials{Email: uMail}, true)
p.mail.Send(mail.MailBody{
ReceiversAddress: uMail,
Subject: "[Quick Note] Password Reset",
Body: fmt.Sprintf(
"<a href=\"%vreset/%v/%v\" rel=\"noreferrer\" target=\"_blank\">Reset</a>",
config.GetEnv("frontend.url"),
id,
token,
),
})
if err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}
RespondWithJSON(w, http.StatusOK, "token sent to email")
}
// Verification
// @Summary verify the user email
// @Tags User
// @Accept json
// @Produce json
// @Param userEmail path string true "email"
// @Success 200 {string} string
// @Failure 404 {string} string
// @Router /user/verification/{userEmail} [get]
func (p *UserHandler) Verification(w http.ResponseWriter, r *http.Request) {
v, httpStatus, err := p.auth.TokenValidation(r, false)
if !v {
RespondWithError(w, httpStatus, err.Error())
return
}
bMail, _ := b64.StdEncoding.DecodeString(chi.URLParam(r, "userEmail"))
uMail := string(bMail)
err = p.repo.Verification(&models.Credentials{Email: uMail})
if err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}
RespondWithJSON(w, http.StatusOK, "successfully email verified")
}
// UpdateUser
// @Summary Update the user
// @Tags User
// @Accept json
// @Produce json
// @Param id path string true "ID"
// @Param data body models.User true "data"
// @Success 200 {string} string "successfully note created"
// @Failure 400 {string} string
// @Failure 500 {string} string
// @Router /user/{id} [put]
func (p *UserHandler) UpdateUser(w http.ResponseWriter, r *http.Request) {
v, httpStatus, err := p.auth.TokenValidation(r, true)
if !v {
RespondWithError(w, httpStatus, err.Error())
return
}
t := models.User{}
ID := chi.URLParam(r, "id")
if err := json.NewDecoder(r.Body).Decode(&t); err != nil {
RespondWithError(w, http.StatusBadRequest, err.Error())
return
}
if err := p.repo.Update(&t, ID, true); err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}
RespondWithJSON(w, http.StatusOK, "successfully updated")
}