Remove public registration, support administrator-managed accounts, and persist personal preferences. Refs #2 #5 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
36 lines
960 B
Go
36 lines
960 B
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/tradarr/backend/internal/auth"
|
|
"github.com/tradarr/backend/internal/httputil"
|
|
)
|
|
|
|
type loginRequest struct {
|
|
Email string `json:"email" binding:"required,email"`
|
|
Password string `json:"password" binding:"required"`
|
|
}
|
|
|
|
func (h *Handler) Login(c *gin.Context) {
|
|
var req loginRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
httputil.BadRequest(c, err)
|
|
return
|
|
}
|
|
user, err := h.repo.GetUserByEmail(req.Email)
|
|
if err != nil || user == nil || !auth.CheckPassword(user.PasswordHash, req.Password) {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid credentials"})
|
|
return
|
|
}
|
|
token, err := auth.GenerateToken(user.ID, user.Email, string(user.Role), h.cfg.JWTSecret)
|
|
if err != nil {
|
|
httputil.InternalError(c, err)
|
|
return
|
|
}
|
|
httputil.OK(c, gin.H{"token": token, "user": user})
|
|
}
|
|
|
|
func (h *Handler) Register(c *gin.Context) { c.Status(http.StatusGone) }
|