Backend fixes: - migration 003: add 'menu' value to recipe_source enum (was causing SQLSTATE 22P02) - migration 004: rename recipe_products→recipe_ingredients, product_id→ingredient_id (was causing SQLSTATE 42P01) - dish/repository.go: fix INSERT INTO tags using $1/$1 for two columns → $1/$2 (was causing SQLSTATE 42P08) - home/handler.go: replace non-existent saved_recipes table with correct joins (recipes→dishes→dish_translations, user_saved_recipes) so today's plan and recommendations load correctly - reqlog: new slog.Handler wrapper that adds request_id and stack trace to ERROR-level logs - all handlers: slog.Error→slog.ErrorContext so error logs include request context; writeError includes request_id in response body Client: - home_screen.dart: extend home screen to future dates, show planned meals as ghost entries - l10n: add new localisation keys for home screen date navigation and planned meal UI Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
111 lines
2.8 KiB
Go
111 lines
2.8 KiB
Go
package auth
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"github.com/food-ai/backend/internal/infra/middleware"
|
|
)
|
|
|
|
const maxRequestBodySize = 1 << 20 // 1 MB
|
|
|
|
type Handler struct {
|
|
service *Service
|
|
}
|
|
|
|
func NewHandler(service *Service) *Handler {
|
|
return &Handler{service: service}
|
|
}
|
|
|
|
type loginRequest struct {
|
|
FirebaseToken string `json:"firebase_token"`
|
|
}
|
|
|
|
type refreshRequest struct {
|
|
RefreshToken string `json:"refresh_token"`
|
|
}
|
|
|
|
func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodySize)
|
|
var req loginRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeErrorJSON(w, r, http.StatusBadRequest, "invalid request body")
|
|
return
|
|
}
|
|
|
|
if req.FirebaseToken == "" {
|
|
writeErrorJSON(w, r, http.StatusBadRequest, "firebase_token is required")
|
|
return
|
|
}
|
|
|
|
resp, err := h.service.Login(r.Context(), req.FirebaseToken)
|
|
if err != nil {
|
|
writeErrorJSON(w, r, http.StatusUnauthorized, "authentication failed")
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, resp)
|
|
}
|
|
|
|
func (h *Handler) Refresh(w http.ResponseWriter, r *http.Request) {
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodySize)
|
|
var req refreshRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeErrorJSON(w, r, http.StatusBadRequest, "invalid request body")
|
|
return
|
|
}
|
|
|
|
if req.RefreshToken == "" {
|
|
writeErrorJSON(w, r, http.StatusBadRequest, "refresh_token is required")
|
|
return
|
|
}
|
|
|
|
resp, err := h.service.Refresh(r.Context(), req.RefreshToken)
|
|
if err != nil {
|
|
writeErrorJSON(w, r, http.StatusUnauthorized, "invalid refresh token")
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, resp)
|
|
}
|
|
|
|
func (h *Handler) Logout(w http.ResponseWriter, r *http.Request) {
|
|
userID := middleware.UserIDFromCtx(r.Context())
|
|
if userID == "" {
|
|
writeErrorJSON(w, r, http.StatusUnauthorized, "unauthorized")
|
|
return
|
|
}
|
|
|
|
if err := h.service.Logout(r.Context(), userID); err != nil {
|
|
writeErrorJSON(w, r, http.StatusInternalServerError, "logout failed")
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
|
}
|
|
|
|
type errorResponse struct {
|
|
Error string `json:"error"`
|
|
RequestID string `json:"request_id,omitempty"`
|
|
}
|
|
|
|
func writeErrorJSON(w http.ResponseWriter, r *http.Request, status int, msg string) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
if encodeErr := json.NewEncoder(w).Encode(errorResponse{
|
|
Error: msg,
|
|
RequestID: middleware.RequestIDFromCtx(r.Context()),
|
|
}); encodeErr != nil {
|
|
slog.ErrorContext(r.Context(), "failed to write error response", "err", encodeErr)
|
|
}
|
|
}
|
|
|
|
func writeJSON(w http.ResponseWriter, status int, v interface{}) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
if err := json.NewEncoder(w).Encode(v); err != nil {
|
|
slog.Error("failed to write JSON response", "err", err)
|
|
}
|
|
}
|