2024-04-21 10:36:48 -07:00
|
|
|
import { AuthResponse } from "../../src/types";
|
2024-07-25 09:48:06 -03:00
|
|
|
import { Logger } from "./logger";
|
2024-04-21 10:36:48 -07:00
|
|
|
|
2024-04-21 11:39:36 -07:00
|
|
|
let warningCount = 0;
|
|
|
|
|
|
2024-04-21 10:36:48 -07:00
|
|
|
export function withAuth<T extends AuthResponse, U extends any[]>(
|
|
|
|
|
originalFunction: (...args: U) => Promise<T>
|
|
|
|
|
) {
|
|
|
|
|
return async function (...args: U): Promise<T> {
|
2024-08-12 14:20:41 -03:00
|
|
|
const useDbAuthentication = process.env.USE_DB_AUTHENTICATION === 'true';
|
|
|
|
|
if (!useDbAuthentication) {
|
2024-04-21 11:39:36 -07:00
|
|
|
if (warningCount < 5) {
|
2024-07-25 09:48:06 -03:00
|
|
|
Logger.warn("You're bypassing authentication");
|
2024-04-21 11:39:36 -07:00
|
|
|
warningCount++;
|
|
|
|
|
}
|
2024-04-21 10:36:48 -07:00
|
|
|
return { success: true } as T;
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
return await originalFunction(...args);
|
|
|
|
|
} catch (error) {
|
2024-07-25 09:48:06 -03:00
|
|
|
Logger.error(`Error in withAuth function: ${error}`);
|
2024-04-21 10:36:48 -07:00
|
|
|
return { success: false, error: error.message } as T;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|