UNPKG

autumn-js

Version:

Autumn JS Library

67 lines (64 loc) 1.45 kB
import { toBackendError, toBackendRes } from "./chunk-IOSEAUDJ.mjs"; import { logger } from "./chunk-45WVZY23.mjs"; // src/libraries/backend/utils/withAuth.ts var withAuth = ({ fn, requireCustomer = true }) => { return async ({ autumn, body, path, getCustomer, pathParams, searchParams }) => { let authResult = await getCustomer(); let customerId = authResult?.customerId; if (!customerId && requireCustomer) { if (body?.errorOnNotFound === false) { return { statusCode: 202, body: null }; } else { logger.error( `[Autumn]: customerId returned from identify function is ${customerId}` ); return toBackendError({ path, message: `customerId returned from identify function is ${customerId}`, code: "no_customer_id", statusCode: 401 }); } } let cusData = authResult?.customerData || body?.customer_data; try { let res = await fn({ body, autumn, customer_id: customerId, customer_data: cusData, pathParams, searchParams }); return toBackendRes({ res }); } catch (error) { logger.error(`${error.message}`); return toBackendError({ path, message: error.message || "unknown error", code: "internal_error" }); } }; }; export { withAuth };