fix: normalize frontend api errors and remove sensitive debug logs
This commit is contained in:
parent
ac92bed8a1
commit
a5f99ba475
@ -12,10 +12,9 @@ app.use(express.json());
|
|||||||
// Serve static files from public directory
|
// Serve static files from public directory
|
||||||
app.use('/test', express.static(path.join(__dirname, 'public')));
|
app.use('/test', express.static(path.join(__dirname, 'public')));
|
||||||
|
|
||||||
const allowedOrigins = process.env.ALLOWED_ORIGINS.split(",").map(origin => origin.trim());
|
const allowedOrigins = process.env.ALLOWED_ORIGINS.split(",").map(origin => origin.trim());
|
||||||
console.log("Allowed Origins:", allowedOrigins);
|
app.use(
|
||||||
app.use(
|
cors({
|
||||||
cors({
|
|
||||||
origin: function (origin, callback) {
|
origin: function (origin, callback) {
|
||||||
if (!origin) return callback(null, true);
|
if (!origin) return callback(null, true);
|
||||||
if (allowedOrigins.includes(origin)) return callback(null, true);
|
if (allowedOrigins.includes(origin)) return callback(null, true);
|
||||||
|
|||||||
@ -100,7 +100,6 @@ exports.markBought = async (req, res) => {
|
|||||||
if (!item_name) return res.status(400).json({ message: "Item name is required" });
|
if (!item_name) return res.status(400).json({ message: "Item name is required" });
|
||||||
|
|
||||||
const item = await List.getItemByName(householdId, storeId, item_name);
|
const item = await List.getItemByName(householdId, storeId, item_name);
|
||||||
console.log('requesting mark ', { item, householdId, storeId, item_name, bought, quantity_bought });
|
|
||||||
if (!item) return res.status(404).json({ message: "Item not found" });
|
if (!item) return res.status(404).json({ message: "Item not found" });
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -97,7 +97,6 @@ exports.getItemByName = async (householdId, storeId, itemName) => {
|
|||||||
AND hl.item_id = $3`,
|
AND hl.item_id = $3`,
|
||||||
[householdId, storeId, itemId]
|
[householdId, storeId, itemId]
|
||||||
);
|
);
|
||||||
console.log(result.rows);
|
|
||||||
return result.rows[0] || null;
|
return result.rows[0] || null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -5,12 +5,10 @@ exports.ROLES = {
|
|||||||
USER: "user",
|
USER: "user",
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.findByUsername = async (username) => {
|
exports.findByUsername = async (username) => {
|
||||||
query = `SELECT * FROM users WHERE username = ${username}`;
|
const result = await pool.query("SELECT * FROM users WHERE username = $1", [username]);
|
||||||
const result = await pool.query("SELECT * FROM users WHERE username = $1", [username]);
|
return result.rows[0];
|
||||||
console.log(query);
|
};
|
||||||
return result.rows[0];
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.createUser = async (username, hashedPassword, name) => {
|
exports.createUser = async (username, hashedPassword, name) => {
|
||||||
const result = await pool.query(
|
const result = await pool.query(
|
||||||
|
|||||||
@ -16,17 +16,24 @@ api.interceptors.request.use((config => {
|
|||||||
return config;
|
return config;
|
||||||
}));
|
}));
|
||||||
|
|
||||||
api.interceptors.response.use(
|
api.interceptors.response.use(
|
||||||
response => response,
|
response => response,
|
||||||
error => {
|
error => {
|
||||||
if (error.response?.status === 401 &&
|
const payload = error.response?.data;
|
||||||
error.response?.data?.message === "Invalid or expired token") {
|
const normalizedMessage = payload?.error?.message || payload?.message;
|
||||||
localStorage.removeItem("token");
|
|
||||||
window.location.href = "/login";
|
if (payload?.error?.message && payload.message === undefined) {
|
||||||
alert("Your session has expired. Please log in again.");
|
payload.message = payload.error.message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (error.response?.status === 401 &&
|
||||||
|
normalizedMessage === "Invalid or expired token") {
|
||||||
|
localStorage.removeItem("token");
|
||||||
|
window.location.href = "/login";
|
||||||
|
alert("Your session has expired. Please log in again.");
|
||||||
|
}
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
export default api;
|
export default api;
|
||||||
|
|||||||
@ -38,7 +38,6 @@ export default function CreateJoinHousehold({ onClose }) {
|
|||||||
setError("");
|
setError("");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log("Joining household with invite code:", inviteCode);
|
|
||||||
await joinHousehold(inviteCode);
|
await joinHousehold(inviteCode);
|
||||||
await refreshHouseholds();
|
await refreshHouseholds();
|
||||||
onClose();
|
onClose();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user