({ request }: LoaderFunctionArgs)
| 18 | .passthrough(); |
| 19 | |
| 20 | export async function loader({ request }: LoaderFunctionArgs) { |
| 21 | if (request.method.toUpperCase() !== "GET") { |
| 22 | return { status: 405, body: "Method Not Allowed" }; |
| 23 | } |
| 24 | |
| 25 | const url = requestUrl(request); |
| 26 | |
| 27 | const parsedParams = ParamsSchema.safeParse(Object.fromEntries(url.searchParams)); |
| 28 | |
| 29 | if (!parsedParams.success) { |
| 30 | // TODO: this should redirect to the integrations page (need to lookup orgid in cookie) |
| 31 | throw new Response("Invalid params", { status: 400 }); |
| 32 | } |
| 33 | |
| 34 | if (parsedParams.data.error) { |
| 35 | // TODO: this should redirect to the integrations page (need to lookup orgid in cookie) |
| 36 | throw new Response(parsedParams.data.error, { status: 400 }); |
| 37 | } |
| 38 | |
| 39 | if (!parsedParams.data.code || !parsedParams.data.state) { |
| 40 | throw new Response("Invalid params", { status: 400 }); |
| 41 | } |
| 42 | |
| 43 | const attempt = await prisma.connectionAttempt.findUnique({ |
| 44 | where: { |
| 45 | id: parsedParams.data.state, |
| 46 | }, |
| 47 | include: { |
| 48 | integration: { |
| 49 | include: { |
| 50 | customClientReference: true, |
| 51 | }, |
| 52 | }, |
| 53 | }, |
| 54 | }); |
| 55 | |
| 56 | if (!attempt) { |
| 57 | throw new Response("Invalid attempt", { status: 400 }); |
| 58 | } |
| 59 | |
| 60 | let customOAuthClient: OAuthClient | undefined; |
| 61 | if (attempt.integration.customClientReference) { |
| 62 | const secretStore = getSecretStore(env.SECRET_STORE); |
| 63 | customOAuthClient = await secretStore.getSecret( |
| 64 | OAuthClientSchema, |
| 65 | attempt.integration.customClientReference.key |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | try { |
| 70 | await integrationAuthRepository.createConnectionFromAttempt({ |
| 71 | attempt, |
| 72 | code: parsedParams.data.code, |
| 73 | url, |
| 74 | customOAuthClient, |
| 75 | }); |
| 76 | |
| 77 | return redirect(attempt.redirectTo); |
nothing calls this directly
no test coverage detected
searching dependent graphs…