Compare commits
2 Commits
a5924086f4
...
0da6d07283
| Author | SHA1 | Date | |
|---|---|---|---|
| 0da6d07283 | |||
| 04bb0e3347 |
+16
-2
@@ -4,6 +4,11 @@ const jwt = require("jsonwebtoken");
|
|||||||
const db = require("./db");
|
const db = require("./db");
|
||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
|
|
||||||
|
function log(level, message, meta = {}) {
|
||||||
|
const timestamp = new Date().toISOString();
|
||||||
|
console.log(JSON.stringify({ timestamp, level, service: "auth_api", message, ...meta }));
|
||||||
|
}
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
@@ -87,12 +92,21 @@ app.post("/login", async (req, res) => {
|
|||||||
if (!ok) return res.status(401).json({ error: "invalid_credentials" });
|
if (!ok) return res.status(401).json({ error: "invalid_credentials" });
|
||||||
|
|
||||||
const isAdmin = ADMIN_USER_CODES.has(Number(user.usercode));
|
const isAdmin = ADMIN_USER_CODES.has(Number(user.usercode));
|
||||||
const payload = { userId: user.id, userCode: user.usercode, isAdmin };
|
const payload = {
|
||||||
|
userId: user.id,
|
||||||
|
userCode: user.usercode,
|
||||||
|
isAdmin: Boolean(isAdmin),
|
||||||
|
};
|
||||||
const token = jwt.sign(payload, JWT_SECRET, { expiresIn: "1h" });
|
const token = jwt.sign(payload, JWT_SECRET, { expiresIn: "1h" });
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
token,
|
token,
|
||||||
user: { id: user.id, userCode: user.usercode, job: user.job, isAdmin },
|
user: {
|
||||||
|
id: user.id,
|
||||||
|
userCode: user.usercode,
|
||||||
|
job: user.job,
|
||||||
|
isAdmin: Boolean(isAdmin),
|
||||||
|
},
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await reportError({
|
await reportError({
|
||||||
|
|||||||
@@ -3,6 +3,11 @@ const bcrypt = require("bcryptjs");
|
|||||||
const db = require("./db");
|
const db = require("./db");
|
||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
|
|
||||||
|
function log(level, message, meta = {}) {
|
||||||
|
const timestamp = new Date().toISOString();
|
||||||
|
console.log(JSON.stringify({ timestamp, level, service: "employee_api", message, ...meta }));
|
||||||
|
}
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
|
|||||||
+65
-5
@@ -5,13 +5,36 @@ require("dotenv").config();
|
|||||||
|
|
||||||
const db = require("./db");
|
const db = require("./db");
|
||||||
|
|
||||||
|
function log(level, message, meta = {}) {
|
||||||
|
const timestamp = new Date().toISOString();
|
||||||
|
console.log(JSON.stringify({ timestamp, level, service: "error_log_api", message, ...meta }));
|
||||||
|
}
|
||||||
|
|
||||||
|
async function ensureErrorLogTable() {
|
||||||
|
try {
|
||||||
|
await db.query(`CREATE SCHEMA IF NOT EXISTS data`);
|
||||||
|
await db.query(`
|
||||||
|
CREATE TABLE IF NOT EXISTS data.errors (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
"date" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
"service" TEXT NOT NULL,
|
||||||
|
partofservice TEXT,
|
||||||
|
error TEXT NOT NULL
|
||||||
|
)
|
||||||
|
`);
|
||||||
|
console.log("Error log table ready");
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Failed to create error log table:", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(express.json({ limit: "64kb" }));
|
app.use(express.json({ limit: "64kb" }));
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
|
|
||||||
const PORT = Number(process.env.PORT || 5003);
|
const ERROR_LOG_API_URL =
|
||||||
const JWT_SECRET = process.env.JWT_SECRET || "";
|
process.env.ERROR_LOG_API_URL ||
|
||||||
// Admin verification is done by `auth_api` and carried in the JWT as `isAdmin`.
|
"https://error.xtransit.testinstance.nl/error_log";
|
||||||
|
|
||||||
function toFriendlyError(statusCode) {
|
function toFriendlyError(statusCode) {
|
||||||
if (statusCode === 400) return "Controleer je invoer en probeer opnieuw.";
|
if (statusCode === 400) return "Controleer je invoer en probeer opnieuw.";
|
||||||
@@ -21,6 +44,40 @@ function toFriendlyError(statusCode) {
|
|||||||
return "Er ging iets mis. Probeer het later opnieuw.";
|
return "Er ging iets mis. Probeer het later opnieuw.";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function reportError({ partofservice, error }) {
|
||||||
|
try {
|
||||||
|
await fetch(ERROR_LOG_API_URL, {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({
|
||||||
|
service: "error_log_api",
|
||||||
|
partofservice,
|
||||||
|
error,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
} catch (_) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
app.use((req, res, next) => {
|
||||||
|
const originalJson = res.json.bind(res);
|
||||||
|
res.json = (body) => {
|
||||||
|
if (res.statusCode >= 400 && body && typeof body === "object") {
|
||||||
|
const rawError =
|
||||||
|
body.error || body.message || body.details || `HTTP ${res.statusCode}`;
|
||||||
|
void reportError({
|
||||||
|
partofservice: `${req.method} ${req.originalUrl}`,
|
||||||
|
error: rawError,
|
||||||
|
});
|
||||||
|
return originalJson({ error: toFriendlyError(res.statusCode) });
|
||||||
|
}
|
||||||
|
return originalJson(body);
|
||||||
|
};
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
const PORT = Number(process.env.PORT || 5003);
|
||||||
|
|
||||||
function normalizeErrorText(value) {
|
function normalizeErrorText(value) {
|
||||||
if (!value) return "Unknown error";
|
if (!value) return "Unknown error";
|
||||||
if (typeof value === "string") return value;
|
if (typeof value === "string") return value;
|
||||||
@@ -58,7 +115,7 @@ function verifyAdmin(req, res, next) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const payload = jwt.verify(auth.slice(7), JWT_SECRET);
|
const payload = jwt.verify(auth.slice(7), JWT_SECRET);
|
||||||
if (!payload?.user.isAdmin) {
|
if (!payload?.isAdmin) {
|
||||||
return res.status(403).json({ error: toFriendlyError(403) });
|
return res.status(403).json({ error: toFriendlyError(403) });
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,6 +209,9 @@ app.use(async (err, req, res, _next) => {
|
|||||||
return res.status(500).json({ error: toFriendlyError(500) });
|
return res.status(500).json({ error: toFriendlyError(500) });
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(PORT, () => console.log(`Error Log API listening on ${PORT}`));
|
app.listen(PORT, async () => {
|
||||||
|
await ensureErrorLogTable();
|
||||||
|
console.log(`Error Log API listening on ${PORT}`);
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = app;
|
module.exports = app;
|
||||||
|
|||||||
@@ -5,6 +5,11 @@ require("dotenv").config();
|
|||||||
const db = require("./db");
|
const db = require("./db");
|
||||||
const { authMiddleware } = require("./middleware");
|
const { authMiddleware } = require("./middleware");
|
||||||
|
|
||||||
|
function log(level, message, meta = {}) {
|
||||||
|
const timestamp = new Date().toISOString();
|
||||||
|
console.log(JSON.stringify({ timestamp, level, service: "transit_api", message, ...meta }));
|
||||||
|
}
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
|
|||||||
Reference in New Issue
Block a user