Standardize JWT authentication and finalize Activity Log V3 migration

This commit is contained in:
Scott Idem
2026-01-07 17:43:23 -05:00
parent 87023e7483
commit ea0d57658f
12 changed files with 199 additions and 109 deletions

View File

@@ -65,29 +65,35 @@ export const get_object = async function get_object({
delete api_cfg['headers']['x-no-account-id'];
}
// Clean the headers
// Clean and merge headers
const headers_cleaned: key_val = {};
for (const prop in headers) {
const merged_headers = { ...api_cfg['headers'], ...headers };
// Standardize all headers to kebab-case and ensure string values
for (const prop in merged_headers) {
const prop_cleaned = prop.replaceAll('_', '-');
if (typeof headers[prop] != 'string') {
headers[prop] = JSON.stringify(headers[prop]);
}
headers_cleaned[prop_cleaned] = headers[prop];
if (log_lvl > 1) {
console.log(`${prop_cleaned}: ${headers_cleaned[prop_cleaned]}`);
let value = merged_headers[prop];
if (value === null || value === undefined) continue;
if (typeof value !== 'string') {
value = JSON.stringify(value);
}
headers_cleaned[prop_cleaned] = value;
}
headers = headers_cleaned;
// Auto-inject Authorization header if JWT is present but header is missing
const jwt = headers_cleaned['jwt'] || headers_cleaned['JWT'] || api_cfg['jwt'];
if (jwt && !headers_cleaned['Authorization'] && !headers_cleaned['authorization']) {
headers_cleaned['Authorization'] = `Bearer ${jwt}`;
}
if (log_lvl > 1) {
console.log('All headers cleaned:', headers);
console.log('Final cleaned headers:', headers_cleaned);
}
const fetchOptions: RequestInit = {
method: 'GET',
headers: {
...api_cfg['headers'],
...headers
},
headers: headers_cleaned,
signal: controller.signal
};

View File

@@ -5,10 +5,11 @@ export const post_blob_percent_completed = temp_post_blob_percent_completed;
export const temp_post_object_percent_completed = 0;
export const post_object_percent_completed = temp_post_object_percent_completed;
// Updated 2024-05-23
// Updated 2026-01-07
export const post_object = async function post_object({
api_cfg = null,
endpoint = '',
headers = {},
params = {},
data = {},
form_data = null,
@@ -23,6 +24,7 @@ export const post_object = async function post_object({
}: {
api_cfg: any;
endpoint: string;
headers?: any;
params?: any;
data?: any;
form_data?: any;
@@ -68,27 +70,38 @@ export const post_object = async function post_object({
// console.log('HERE!! API POST 2');
// Clean the headers
// Clean and merge headers
const headers_cleaned: Record<string, string> = {};
for (const prop in api_cfg['headers']) {
const merged_headers = { ...api_cfg['headers'], ...headers };
for (const prop in merged_headers) {
const prop_cleaned = prop.replaceAll('_', '-');
headers_cleaned[prop_cleaned] = api_cfg['headers'][prop];
let value = merged_headers[prop];
if (value === null || value === undefined) continue;
if (typeof value !== 'string') {
value = JSON.stringify(value);
}
headers_cleaned[prop_cleaned] = value;
}
// console.log('HERE!! API POST 3');
// Auto-inject Authorization header if JWT is present but header is missing
const jwt = headers_cleaned['jwt'] || headers_cleaned['JWT'] || api_cfg['jwt'];
if (jwt && !headers_cleaned['Authorization'] && !headers_cleaned['authorization']) {
headers_cleaned['Authorization'] = `Bearer ${jwt}`;
}
if (form_data) {
// headers_cleaned['Content-Type'] = 'multipart/form-data';
delete headers_cleaned['content-type'];
delete headers_cleaned['Content-Type'];
delete headers_cleaned['content-type']; // Just in case
delete headers_cleaned['Content-type']; // Just in case
console.log('Form Data:', form_data);
} else {
headers_cleaned['Content-Type'] = 'application/json';
}
if (log_lvl > 1) {
console.log('Cleaned Headers:', headers_cleaned);
console.log('Final cleaned headers:', headers_cleaned);
}
// console.log('HERE!! API POST 4');
@@ -108,6 +121,8 @@ export const post_object = async function post_object({
signal: controller.signal
};
console.log('Final fetch options for post_object:', fetchOptions);
if (log_lvl > 1) {
console.log('Fetch Options:', fetchOptions);
}