forked from baron/baron-sso
SMS 인증을 위한 Naver SENS 연동 및 API 구현
This commit is contained in:
@@ -2,27 +2,61 @@ package handler
|
||||
|
||||
import (
|
||||
"baron-sso-backend/internal/domain"
|
||||
"baron-sso-backend/internal/service"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type AuthHandler struct {
|
||||
ProjectID string
|
||||
ProjectID string
|
||||
SmsService domain.SmsService
|
||||
}
|
||||
|
||||
func NewAuthHandler() *AuthHandler {
|
||||
pid := os.Getenv("DESCOPE_PROJECT_ID")
|
||||
if pid == "" {
|
||||
// Fallback for dev if not set
|
||||
pid = "P37DsGepBT6uDWb5TYYpb5RxUPuq"
|
||||
pid = "P37DsGepBT6uDWb5TYYpb5RxUPuq"
|
||||
}
|
||||
return &AuthHandler{ProjectID: pid}
|
||||
return &AuthHandler{
|
||||
ProjectID: pid,
|
||||
SmsService: service.NewSmsService(),
|
||||
}
|
||||
}
|
||||
|
||||
// SendSms sends a verification code via SMS.
|
||||
func (h *AuthHandler) SendSms(c *fiber.Ctx) error {
|
||||
var req domain.SmsRequest
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request body"})
|
||||
}
|
||||
|
||||
// Sanitize phone number: remove dashes
|
||||
sanitizedPhone := strings.ReplaceAll(req.PhoneNumber, "-", "")
|
||||
|
||||
// Generate a 6-digit verification code
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
code := fmt.Sprintf("%06d", rand.Intn(1000000))
|
||||
content := fmt.Sprintf("[Baron SSO] Your verification code is %s", code)
|
||||
|
||||
if err := h.SmsService.SendSms(sanitizedPhone, content); err != nil {
|
||||
log.Printf("Error sending SMS: %v", err)
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Failed to send SMS"})
|
||||
}
|
||||
|
||||
// TODO: Store the verification code for later verification
|
||||
|
||||
return c.JSON(fiber.Map{"message": "SMS sent successfully"})
|
||||
}
|
||||
|
||||
// getBaseURL extracts the region code from Project ID if present (e.g., P37... -> api.37ds.descope.com)
|
||||
|
||||
Reference in New Issue
Block a user