1
0
Fork 0
mirror of https://codeberg.org/MarkusThielker/next-ory.git synced 2025-04-10 11:58:41 +00:00
next-ory/dashboard/src/lib/action/client.ts
2025-02-08 23:42:58 +01:00

29 lines
886 B
TypeScript

'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<typeof clientFormSchema>,
) {
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 });
}