feat: 管理后台登录
Some checks failed
Backend Deploy (Go + Docker) / deploy (push) Successful in 1m4s
Extension Build & Release / build (push) Failing after 46s

This commit is contained in:
zs
2026-03-02 23:54:59 +08:00
parent 4e5147fb13
commit d2b330c0c9
7 changed files with 115 additions and 5 deletions

View File

@@ -0,0 +1,44 @@
package handler
import (
"encoding/json"
"net/http"
"github.com/zs/InsightReply/internal/service"
)
type AuthHandler struct {
svc *service.AuthService
}
func NewAuthHandler(svc *service.AuthService) *AuthHandler {
return &AuthHandler{svc: svc}
}
// Login handles user authentication and returns a JWT token
func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) {
var body struct {
Email string `json:"email"`
Password string `json:"password"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
SendError(w, http.StatusBadRequest, 4001, "Invalid request body")
return
}
if body.Email == "" || body.Password == "" {
SendError(w, http.StatusBadRequest, 4001, "Email and Password are required")
return
}
token, err := h.svc.Login(body.Email, body.Password)
if err != nil {
SendError(w, http.StatusUnauthorized, 4001, err.Error())
return
}
SendSuccess(w, map[string]string{
"token": token,
})
}