package server import ( "database/sql" "encoding/json" "net/http" "time" "git.wntrmute.dev/kyle/eng-pad-server/internal/auth" ) type loginRequest struct { Username string `json:"username"` Password string `json:"password"` } type loginResponse struct { Token string `json:"token"` } func handleLogin(database *sql.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req loginRequest if err := json.NewDecoder(r.Body).Decode(&req); err != nil { http.Error(w, `{"error":"invalid request"}`, http.StatusBadRequest) return } userID, err := auth.AuthenticateUser(database, req.Username, req.Password) if err != nil { http.Error(w, `{"error":"invalid credentials"}`, http.StatusUnauthorized) return } token, err := auth.CreateToken(database, userID, 24*time.Hour) if err != nil { http.Error(w, `{"error":"internal error"}`, http.StatusInternalServerError) return } // Set session cookie http.SetCookie(w, &http.Cookie{ Name: "session", Value: token, Path: "/", HttpOnly: true, Secure: true, SameSite: http.SameSiteStrictMode, MaxAge: 86400, }) w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(loginResponse{Token: token}) } }