Add phone registration with SMS verification and regenerate Swagger docs.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -8,15 +8,18 @@ import (
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/handlers"
|
||||
"online-back/pkg/logging"
|
||||
"online-back/pkg/sms"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jackc/pgx/v4"
|
||||
)
|
||||
|
||||
const (
|
||||
loginURL = "/login"
|
||||
tokenURL = "/token"
|
||||
profileURL = "/profile"
|
||||
loginURL = "/login"
|
||||
tokenURL = "/token"
|
||||
profileURL = "/profile"
|
||||
registerURL = "/register"
|
||||
verifyURL = "/register/verify"
|
||||
)
|
||||
|
||||
type handler struct {
|
||||
@@ -25,13 +28,15 @@ type handler struct {
|
||||
service *Service
|
||||
}
|
||||
|
||||
func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler {
|
||||
return &handler{logger: logger, repository: repository, service: NewService(repository)}
|
||||
func NewHandler(logger *logging.Logger, repository Repository, sender sms.Sender) handlers.Handler {
|
||||
return &handler{logger: logger, repository: repository, service: NewService(repository, sender)}
|
||||
}
|
||||
|
||||
func (h *handler) Register(router *gin.RouterGroup) {
|
||||
router.POST(loginURL, h.login)
|
||||
router.POST(tokenURL, h.refreshToken)
|
||||
router.POST(registerURL, h.register)
|
||||
router.POST(verifyURL, h.verify)
|
||||
|
||||
protected := router.Group("", appresult.JwtTokenCheck())
|
||||
protected.GET(profileURL, h.getProfile)
|
||||
@@ -69,6 +74,76 @@ func (h *handler) login(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(tokenPair))
|
||||
}
|
||||
|
||||
// register godoc
|
||||
// @Summary Register
|
||||
// @Description Starts registration and sends an SMS verification code. Replace sms.LogSender with your SMS client.
|
||||
// @Tags auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body RegisterDTO true "Registration payload"
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 409 {object} appresult.AppError
|
||||
// @Router /register [post]
|
||||
func (h *handler) register(c *gin.Context) {
|
||||
var dto RegisterDTO
|
||||
if err := c.ShouldBindJSON(&dto); err != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.service.Register(c.Request.Context(), dto)
|
||||
if err != nil {
|
||||
h.respondRegisterError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
|
||||
}
|
||||
|
||||
// verify godoc
|
||||
// @Summary Verify phone
|
||||
// @Description Confirms the SMS code and creates the user account
|
||||
// @Tags auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body VerifyDTO true "Verification payload"
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 409 {object} appresult.AppError
|
||||
// @Router /register/verify [post]
|
||||
func (h *handler) verify(c *gin.Context) {
|
||||
var dto VerifyDTO
|
||||
if err := c.ShouldBindJSON(&dto); err != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
tokenPair, err := h.service.Verify(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, ErrConflict):
|
||||
c.JSON(http.StatusConflict, appresult.ErrConflict)
|
||||
case errors.Is(err, ErrInvalidCode):
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrInvalidCode)
|
||||
default:
|
||||
h.logger.Errorf("register/verify failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
}
|
||||
}
|
||||
|
||||
// refreshToken godoc
|
||||
// @Summary Refresh tokens
|
||||
// @Description Issues a new access and refresh token pair
|
||||
|
||||
Reference in New Issue
Block a user