From 7da7a3c8ca957fae0701760573ea5af85fa84c8b Mon Sep 17 00:00:00 2001 From: Markus Thielker Date: Sun, 26 Jan 2025 20:33:21 +0100 Subject: [PATCH] NORY-46: add basic action to create client --- dashboard/src/lib/action/client.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 dashboard/src/lib/action/client.ts diff --git a/dashboard/src/lib/action/client.ts b/dashboard/src/lib/action/client.ts new file mode 100644 index 0000000..5cc2799 --- /dev/null +++ b/dashboard/src/lib/action/client.ts @@ -0,0 +1,29 @@ +'use server'; + +import { clientFormSchema } from '@/lib/forms/client-form'; +import { z } from 'zod'; +import { getFrontendApi, getOAuth2Api } from '@/ory/sdk/server'; +import { cookies } from 'next/headers'; + +export async function createClient( + formData: z.infer, +) { + + const cookie = await cookies(); + const frontendApi = await getFrontendApi(); + + const session = await frontendApi + .toSession({ cookie: 'ory_kratos_session=' + cookie.get('ory_kratos_session')?.value }) + .then((response) => response.data) + .catch(() => null); + + if (!session) { + console.log('Unauthorised action call'); + throw 'Unauthorised'; + } + + console.log(session.identity?.traits.email, 'posted form', formData); + + const oauthApi = await getOAuth2Api(); + return await oauthApi.createOAuth2Client({ oAuth2Client: formData }); +}