-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
215 lines (191 loc) · 6.78 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
package user
import (
"errors"
"net/http"
"net/url"
"github.com/dominiclet/golang-base/lib/httpresp"
"github.com/dominiclet/golang-base/lib/resperror"
"github.com/dominiclet/golang-base/service/user"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
type UserHandler struct {
userService *user.UserService
}
func InitUserHandler(userService *user.UserService) *UserHandler {
return &UserHandler{
userService,
}
}
// @Summary Create user
// @Description Create new user
// @Tags user
// @Accept json
// @Param req body CreateUserRequest true "Create user data"
// @Produce json
// @Failure 409 {object} httpresp.StandardResponse "User with same email already exists"
// @Success 200 {object} httpresp.StandardDataResponse{data=CreateUserResponse}
// @Router /user [post]
func (h *UserHandler) CreateUser(c *gin.Context) {
var userReq CreateUserRequest
if err := c.ShouldBindJSON(&userReq); err != nil {
httpresp.SendError(c, resperror.NewError(resperror.BadRequest))
return
}
newUser, err := h.userService.CreateUser(c, userReq.Name,
userReq.Email, userReq.Password)
if err != nil {
httpresp.SendError(c, err)
return
}
httpresp.SendData(c, CreateUserResponse{
ID: newUser.ID,
}, http.StatusOK)
}
// @Summary Get basic user information
// @Description Get basic user information (protected endpoint)
// @Tags user,authRequired
// @Param uuid query string true "UUID"
// @Success 200 {object} httpresp.StandardDataResponse{data=User}
// @Failure 404 {object} httpresp.StandardResponse "User not found"
// @Router /user/{uuid} [get]
func (h *UserHandler) GetUser(c *gin.Context) {
uuid := c.Param("uuid")
decodedUUID, err := url.QueryUnescape(uuid)
if err != nil {
httpresp.SendError(c, resperror.NewError(resperror.BadRequest))
return
}
user, err := h.userService.GetUserByUuid(c, decodedUUID)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
httpresp.SendError(c, resperror.NewError(resperror.UserNotFound))
return
}
httpresp.SendErrorWithStatusCode(c, err, http.StatusInternalServerError)
return
}
respUser := NewUserFromSvcUser(user)
httpresp.SendData(c, respUser, http.StatusOK)
}
// @Summary Verify email
// @Description Handles verification link for email
// @Tags user
// @Param userUuid query int true "User ID"
// @Param token query string true "Verification token"
// @Router /user/verify/{userUuid}/{token} [get]
func (h *UserHandler) VerifyEmail(c *gin.Context) {
userUUID := c.Param("userUuid")
verificationToken := c.Param("token")
decodedUUID, err := url.QueryUnescape(userUUID)
if err != nil {
c.String(http.StatusBadRequest, "Verification failed")
return
}
err = h.userService.VerifyEmail(c, decodedUUID, verificationToken)
if err != nil {
c.String(http.StatusForbidden, "Verification failed")
return
}
c.String(http.StatusOK, "Verification successful. Please head back to the portal to login.")
}
// @Summary Resned verification email
// @Description Resends verification email while invalidating previous verification link
// @Tags user
// @Param req body ResendVerificationEmailRequest true "Email and password must be provided to authenticate before sending"
// @Accept json
// @Produce json
// @Success 200 {object} httpresp.StandardResponse
// @Failure 429 {object} httpresp.StandardResponse "Too many requests made in a short period of time (2 mins)"
// @Failure 405 {object} httpresp.StandardResponse "User already verified"
// @Router /user/verify/resend_email [post]
func (h *UserHandler) ResendVerificationEmail(c *gin.Context) {
var req ResendVerificationEmailRequest
if err := c.ShouldBindJSON(&req); err != nil {
httpresp.SendError(c, resperror.NewError(resperror.BadRequest))
return
}
err := h.userService.ResendVerificationEmail(c, req.Email, req.Password)
if err != nil {
if expectedErr, ok := err.(user.SendVerificationEmailError); ok {
switch expectedErr.ErrType() {
case user.ResendDisabled:
httpresp.SendError(c, resperror.NewError(resperror.TooManyRequests))
return
case user.UserVerified:
httpresp.SendError(c, resperror.NewError(resperror.UserAlreadyVerifiedError))
return
}
}
// Just return 500 internal server error by default
httpresp.SendError(c, err)
return
}
httpresp.SendSuccess(c)
}
// @Summary Reset password
// @Description Starts reset password process by generating and sending 6-digit token to provided email if account exists
// @Tags user
// @Accept json
// @Param req body ResetPasswordRequest true "Specify email of account to reset"
// @Produce json
// @Success 200 {object} httpresp.StandardResponse
// @Router /user/reset_password [post]
func (h *UserHandler) ResetPassword(c *gin.Context) {
var req ResetPasswordRequest
if err := c.ShouldBindJSON(&req); err != nil {
httpresp.SendError(c, resperror.NewError(resperror.BadRequest))
return
}
// Error is intentionally not handled
h.userService.ResetPassword(c, req.Email)
// Handler always returns success to mask any errors from FE
httpresp.SendSuccess(c)
}
// @Summary Reset password auth code exchange
// @Description Verify 6-digit token and exchange token with auth code to set new password
// @Tags user
// @Accept json
// @Param req body ResetPWAuthCodeExchangeRequest true "Specify both email and token"
// @Produce json
// @Failure 401 {object} httpresp.StandardResponse "Token rejected"
// @Success 200 {object} httpresp.StandardDataResponse{data=ResetPWAuthCodeExchangeResponse}
// @Router /user/reset_password/token_exchange [post]
func (h *UserHandler) ResetPasswordAuthCodeExchange(c *gin.Context) {
var req ResetPWAuthCodeExchangeRequest
if err := c.ShouldBindJSON(&req); err != nil {
httpresp.SendError(c, resperror.NewError(resperror.BadRequest))
return
}
authCode, err := h.userService.ResetPwAuthCodeExchange(c, req.Token, req.Email)
if err != nil {
httpresp.SendError(c, resperror.NewError(resperror.InvalidTokenError))
return
}
httpresp.SendData(c, ResetPWAuthCodeExchangeResponse{
AuthCode: authCode,
}, http.StatusOK)
}
// Final API call for reset password process (sets new password)
// @Summary Set new password
// @Description Final API call for reset password process. Set new password, authenticating using auth code.
// @Tags user
// @Accept json
// @Param req body SetNewPWRequest true "Specify new password for account"
// @Produce json
// @Failure 401 {object} httpresp.StandardResponse "Auth code rejected"
// @Success 200 {object} httpresp.StandardResponse
// @Router /user/reset_password/set_password [post]
func (h *UserHandler) SetNewPassword(c *gin.Context) {
var req SetNewPWRequest
if err := c.ShouldBindJSON(&req); err != nil {
httpresp.SendError(c, resperror.NewError(resperror.BadRequest))
return
}
err := h.userService.SetNewPassword(c, req.Email, req.AuthCode, req.NewPassword)
if err != nil {
httpresp.SendError(c, resperror.NewError(resperror.Unauthorized))
return
}
httpresp.SendSuccess(c)
}