feat: add http handlers, docs
This commit is contained in:
@@ -28,3 +28,16 @@ type RefreshTokenRequest struct {
|
||||
type OTPProviderReq struct {
|
||||
Receptor string `json:"receptor"`
|
||||
}
|
||||
|
||||
type OTPProviderResponse struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type OTPVerifyRequest struct {
|
||||
Phone string `json:"phone" validate:"required"`
|
||||
Code string `json:"code" validate:"required"`
|
||||
}
|
||||
|
||||
type OTPVerifyResponse struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
@@ -91,6 +91,68 @@ func (h *AuthHandler) Authenticate(c *fiber.Ctx) error {
|
||||
})
|
||||
}
|
||||
|
||||
// SendOTP sends OTP code to the provided phone number
|
||||
// @Summary Send OTP code
|
||||
// @Description Send OTP code to the provided phone number
|
||||
// @Tags auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body dto.OTPProviderReq true "OTP Request"
|
||||
// @Success 200 {object} dto.OTPProviderResponse
|
||||
// @Failure 400 {object} map[string]string
|
||||
// @Failure 500 {object} map[string]string
|
||||
// @Router /auth/send-otp [post]
|
||||
func (h *AuthHandler) SendOTP(c *fiber.Ctx) error {
|
||||
var req dto.OTPProviderReq
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"error": "invalid request body",
|
||||
})
|
||||
}
|
||||
|
||||
_, err := h.authService.SendOTPCode(c.Context(), req.Receptor)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(dto.OTPProviderResponse{
|
||||
Message: "OTP code sent successfully",
|
||||
})
|
||||
}
|
||||
|
||||
// VerifyOTP verifies the OTP code
|
||||
// @Summary Verify OTP code
|
||||
// @Description Verify the provided OTP code for the phone number
|
||||
// @Tags auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body dto.OTPVerifyRequest true "OTP Verify Request"
|
||||
// @Success 200 {object} dto.OTPVerifyResponse
|
||||
// @Failure 400 {object} map[string]string
|
||||
// @Failure 401 {object} map[string]string
|
||||
// @Router /auth/verify-otp [post]
|
||||
func (h *AuthHandler) VerifyOTP(c *fiber.Ctx) error {
|
||||
var req dto.OTPVerifyRequest
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"error": "invalid request body",
|
||||
})
|
||||
}
|
||||
|
||||
err := h.authService.VerifyOTP(c.Context(), req.Phone, req.Code)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(dto.OTPVerifyResponse{
|
||||
Message: "OTP verified successfully",
|
||||
})
|
||||
}
|
||||
|
||||
func (h *AuthHandler) HelloWorld(c *fiber.Ctx) error {
|
||||
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
||||
"message": "Hello, World!",
|
||||
|
||||
@@ -47,4 +47,6 @@ func registerPublicRoutes(router fiber.Router, app *app.AppContainer) {
|
||||
// Register auth routes
|
||||
authgroup.Post("/challenge", authHandler.GenerateChallenge)
|
||||
authgroup.Post("/authenticate", authHandler.Authenticate)
|
||||
authgroup.Post("/send-otp", authHandler.SendOTP)
|
||||
authgroup.Post("/verify-otp", authHandler.VerifyOTP)
|
||||
}
|
||||
|
||||
@@ -21,6 +21,8 @@ import (
|
||||
type AuthService interface {
|
||||
GenerateChallenge(ctx context.Context, pubKey string) (*domain.Challenge, error)
|
||||
Authenticate(ctx context.Context, pubKey string, signature string, chainID uint, ipAddress, userAgent string) (*UserToken, error)
|
||||
SendOTPCode(ctx context.Context, phone string) (string, error)
|
||||
VerifyOTP(ctx context.Context, phone, code string) error
|
||||
}
|
||||
|
||||
type authService struct {
|
||||
|
||||
Reference in New Issue
Block a user