2024-04-21 13:36:48 -04:00
|
|
|
import { AuthResponse } from "../../src/types";
|
|
|
|
|
2024-04-21 14:39:36 -04:00
|
|
|
let warningCount = 0;
|
|
|
|
|
2024-04-21 13:36:48 -04:00
|
|
|
export function withAuth<T extends AuthResponse, U extends any[]>(
|
|
|
|
originalFunction: (...args: U) => Promise<T>
|
|
|
|
) {
|
|
|
|
return async function (...args: U): Promise<T> {
|
|
|
|
if (process.env.USE_DB_AUTHENTICATION === "false") {
|
2024-04-21 14:39:36 -04:00
|
|
|
if (warningCount < 5) {
|
|
|
|
console.warn("WARNING - You're bypassing authentication");
|
|
|
|
warningCount++;
|
|
|
|
}
|
2024-04-21 13:36:48 -04:00
|
|
|
return { success: true } as T;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
return await originalFunction(...args);
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error in withAuth function: ", error);
|
|
|
|
return { success: false, error: error.message } as T;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|