axxerion-api/modules/auth.js

43 lines
1023 B
JavaScript

const query = require("../db.js");
async function checkAuthKeys(reqClient, reqApiKey, reqTableName) {
try {
const result = await query(
`select data from admin_api_keys where id = $1 and data['client'] = '"${reqClient}"'`,
[reqApiKey]
);
if (result.rows.length > 0) {
const allowedTables = result.rows[0].data.tables.map(t => t.table);
if (allowedTables.includes(reqTableName)) {
return true;
}
} else {
return false;
}
} catch (err) {
console.error("Error checking auth key:", err);
return false;
}
}
async function checkAdminAuthKey(reqApiKey) {
try {
const result = await query(
`select data from admin_api_keys where id = $1 and data['client'] = '"admin"'`,
[reqApiKey]
);
if (result.rows.length > 0) {
return true;
} else {
return false;
}
} catch (err) {
console.error("Error checking auth key:", err);
return false;
}
}
module.exports = { checkAuthKeys, checkAdminAuthKey };