Add forgot-password SMS reset, client/staff user lists, and regenerate Swagger.
Fold follow-up migrations into init, expose GET /users/clients and /users/staff, and refresh seed data. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -15,11 +15,13 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
loginURL = "/login"
|
||||
tokenURL = "/token"
|
||||
profileURL = "/profile"
|
||||
registerURL = "/register"
|
||||
verifyURL = "/register/verify"
|
||||
loginURL = "/login"
|
||||
tokenURL = "/token"
|
||||
profileURL = "/profile"
|
||||
registerURL = "/register"
|
||||
verifyURL = "/register/verify"
|
||||
forgotPasswordURL = "/forgot-password"
|
||||
resetPasswordURL = "/forgot-password/verify"
|
||||
)
|
||||
|
||||
type handler struct {
|
||||
@@ -37,6 +39,8 @@ func (h *handler) Register(router *gin.RouterGroup) {
|
||||
router.POST(tokenURL, h.refreshToken)
|
||||
router.POST(registerURL, h.register)
|
||||
router.POST(verifyURL, h.verify)
|
||||
router.POST(forgotPasswordURL, h.forgotPassword)
|
||||
router.POST(resetPasswordURL, h.resetPassword)
|
||||
|
||||
protected := router.Group("", appresult.JwtTokenCheck())
|
||||
protected.GET(profileURL, h.getProfile)
|
||||
@@ -45,7 +49,7 @@ func (h *handler) Register(router *gin.RouterGroup) {
|
||||
|
||||
// login godoc
|
||||
// @Summary Login
|
||||
// @Description Authenticates a user by email and password
|
||||
// @Description Authenticates a user by name or phone, and password
|
||||
// @Tags auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
@@ -62,7 +66,12 @@ func (h *handler) login(c *gin.Context) {
|
||||
}
|
||||
|
||||
tokenPair, err := h.service.Login(c.Request.Context(), dto)
|
||||
if errors.Is(err, ErrInvalidInput) {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
if errors.Is(err, ErrInvalidCredentials) {
|
||||
h.logger.Warnf("login failed for name=%q phone=%q: %v", dto.Name, dto.Phone, err)
|
||||
c.JSON(http.StatusUnauthorized, appresult.ErrUnauthorized)
|
||||
return
|
||||
}
|
||||
@@ -128,12 +137,68 @@ func (h *handler) verify(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(tokenPair))
|
||||
}
|
||||
|
||||
// forgotPassword godoc
|
||||
// @Summary Forgot password
|
||||
// @Description Finds the user by name and sends an SMS verification code
|
||||
// @Tags auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body ForgotPasswordDTO true "Forgot password payload"
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 404 {object} appresult.AppError
|
||||
// @Router /forgot-password [post]
|
||||
func (h *handler) forgotPassword(c *gin.Context) {
|
||||
var dto ForgotPasswordDTO
|
||||
if err := c.ShouldBindJSON(&dto); err != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.service.ForgotPassword(c.Request.Context(), dto)
|
||||
if err != nil {
|
||||
h.respondRegisterError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
|
||||
}
|
||||
|
||||
// resetPassword godoc
|
||||
// @Summary Reset password
|
||||
// @Description Confirms the SMS code and sets a new password
|
||||
// @Tags auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body ResetPasswordDTO true "Reset password payload"
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 404 {object} appresult.AppError
|
||||
// @Router /forgot-password/verify [post]
|
||||
func (h *handler) resetPassword(c *gin.Context) {
|
||||
var dto ResetPasswordDTO
|
||||
if err := c.ShouldBindJSON(&dto); err != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
tokenPair, err := h.service.ResetPassword(c.Request.Context(), dto)
|
||||
if err != nil {
|
||||
h.respondRegisterError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(tokenPair))
|
||||
}
|
||||
|
||||
func (h *handler) respondRegisterError(c *gin.Context, err error) {
|
||||
switch {
|
||||
case errors.Is(err, ErrPasswordMismatch):
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrPasswordMismatch)
|
||||
case errors.Is(err, ErrInvalidPhone), errors.Is(err, ErrInvalidInput):
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
case errors.Is(err, ErrNotFound):
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
case errors.Is(err, ErrConflict):
|
||||
c.JSON(http.StatusConflict, appresult.ErrConflict)
|
||||
case errors.Is(err, ErrInvalidCode):
|
||||
|
||||
Reference in New Issue
Block a user