{"version":3,"file":"authorize.mjs","names":[],"sources":["../../../../../../src/astro/routes/api/oauth/device/authorize.ts"],"sourcesContent":["/**\n * POST /_emdash/api/oauth/device/authorize\n *\n * User submits the user code after logging in via the browser.\n * This endpoint requires authentication (the user must be logged in).\n */\n\n/// <reference types=\"@premium-cms/emdash/locals\" />\n\nimport type { APIRoute } from \"astro\";\nimport { z } from \"zod\";\n\nimport { apiError, handleError, unwrapResult } from \"#api/error.js\";\nimport { handleDeviceAuthorize } from \"#api/handlers/device-flow.js\";\nimport { isParseError, parseBody } from \"#api/parse.js\";\n\nexport const prerender = false;\n\nconst authorizeSchema = z.object({\n\tuser_code: z.string().min(1),\n\taction: z.enum([\"approve\", \"deny\"]).optional(),\n});\n\nexport const POST: APIRoute = async ({ request, locals }) => {\n\tconst { emdash } = locals;\n\tconst { user } = locals;\n\n\tif (!emdash?.db) {\n\t\treturn apiError(\"NOT_CONFIGURED\", \"EmDash is not initialized\", 500);\n\t}\n\n\tif (!user) {\n\t\treturn apiError(\"NOT_AUTHENTICATED\", \"Authentication required\", 401);\n\t}\n\n\ttry {\n\t\tconst body = await parseBody(request, authorizeSchema);\n\t\tif (isParseError(body)) return body;\n\n\t\tconst result = await handleDeviceAuthorize(emdash.db, user.id, { role: user.role, roleId: user.roleId ?? null }, body);\n\t\treturn unwrapResult(result);\n\t} catch (error) {\n\t\treturn handleError(error, \"Failed to authorize device\", \"AUTHORIZE_ERROR\");\n\t}\n};\n"],"mappings":";;;;;;;;;;;;AAgBA,MAAa,YAAY;AAEzB,MAAM,kBAAkB,EAAE,OAAO;CAChC,WAAW,EAAE,QAAQ,CAAC,IAAI,EAAE;CAC5B,QAAQ,EAAE,KAAK,CAAC,WAAW,OAAO,CAAC,CAAC,UAAU;CAC9C,CAAC;AAEF,MAAa,OAAiB,OAAO,EAAE,SAAS,aAAa;CAC5D,MAAM,EAAE,WAAW;CACnB,MAAM,EAAE,SAAS;AAEjB,KAAI,CAAC,QAAQ,GACZ,QAAO,SAAS,kBAAkB,6BAA6B,IAAI;AAGpE,KAAI,CAAC,KACJ,QAAO,SAAS,qBAAqB,2BAA2B,IAAI;AAGrE,KAAI;EACH,MAAM,OAAO,MAAM,UAAU,SAAS,gBAAgB;AACtD,MAAI,aAAa,KAAK,CAAE,QAAO;AAG/B,SAAO,aADQ,MAAM,sBAAsB,OAAO,IAAI,KAAK,IAAI;GAAE,MAAM,KAAK;GAAM,QAAQ,KAAK,UAAU;GAAM,EAAE,KAAK,CAC3F;UACnB,OAAO;AACf,SAAO,YAAY,OAAO,8BAA8B,kBAAkB"}