import { DATA_FUNCTION } from "@idot-digital/function-generator"; import { LAYER_SERVER } from "@idot-digital/function-generator"; import { SERVER_REQUIRED, SERVER_TRANSFORMER as TR, SERVER_SETTINGS, ERROR_401, } from "./auth0.js"; export const Auth0Server: LAYER_SERVER = { create(settings) { return { info: { name: "Auth0", path: "./layer/Auth0/auth0", }, attach(next: DATA_FUNCTION) { return { async run( req: TR["REQ"], opt: TR["OPT"] ): Promise["RES"]> { const { req: request } = opt; let uid: string = ""; if ( !request.oidc.isAuthenticated() && settings.allow_unauthorized !== true ) return ERROR_401; if (request.oidc.isAuthenticated()) { // @ts-ignore uid = request.oidc.user.sub; } return next(req, { ...opt, uid, }); }, }; }, }; }, };