diff --git a/apps/web/src/components/pages/auth/registration.tsx b/apps/web/src/components/pages/auth/registration.tsx index 5634337ab9..e87e53f7d7 100644 --- a/apps/web/src/components/pages/auth/registration.tsx +++ b/apps/web/src/components/pages/auth/registration.tsx @@ -91,7 +91,6 @@ export function Registration() { }) .then(() => { setIsSuccessfullySubmitted(true) - window.scrollTo({ top: 0, behavior: 'smooth' }) }) .catch( handleFlowError( @@ -103,6 +102,7 @@ export function Registration() { ) .finally(() => { nProgress.done() + window.scrollTo({ top: 0, behavior: 'smooth' }) }) } diff --git a/apps/web/src/pages/api/.ory/[...paths].ts b/apps/web/src/pages/api/.ory/[...paths].ts index def9a98357..233033b14e 100644 --- a/apps/web/src/pages/api/.ory/[...paths].ts +++ b/apps/web/src/pages/api/.ory/[...paths].ts @@ -1,4 +1,5 @@ import { createApiHandler, config } from '@ory/integrations/next-edge' +import { NextApiRequest, NextApiResponse } from 'next/types' export { config } @@ -8,6 +9,11 @@ const COOKIE_DOMAINS = { local: 'localhost', } +const API_KRATOS_WEBHOOK_URL = + process.env.NEXT_PUBLIC_ENV === 'production' + ? 'https://api.serlo.org/kratos/register' + : 'https://api.serlo-staging.dev/kratos/register' + export const COOKIE_DOMAIN = process.env.NEXT_PUBLIC_ENV === 'production' ? COOKIE_DOMAINS['production'] @@ -27,8 +33,57 @@ const KRATOS_HOST = process.env.NEXT_PUBLIC_ENV ? process.env.NEXT_PUBLIC_ENV : 'staging' ] -export default createApiHandler({ - apiBaseUrlOverride: KRATOS_HOST, - forceCookieSecure: true, - forceCookieDomain: COOKIE_DOMAIN, -}) +export default async function customCreateApiHandler( + req: NextApiRequest, + res: NextApiResponse +): Promise { + // injecting a function here to trigger an api call because + // unfortunately the kratos webhook ist not reliable atm. + function afterRegisterApiCall(body: string) { + if ( + req.method !== 'POST' || + !req.url?.startsWith('/api/.ory/self-service/registration?flow=') || + !process.env.API_KRATOS_SECRET || + process.env.NEXT_PUBLIC_ENV === 'production' + ) { + return + } + + const result = JSON.parse(body) as { identity: { id: string } } + const userId = result?.identity?.id + if (userId) { + void fetch(API_KRATOS_WEBHOOK_URL, { + method: 'POST', + headers: { + 'x-kratos-key': process.env.API_KRATOS_SECRET, + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ userId }), + }) + .then(async (result) => { + if (result.status !== 200) { + const text = await result.text() + console.log(result.status) + console.log({ userId }) + console.error(text) + } + }) + .catch((e) => { + console.error(e) + }) + } + } + + function sendOverwrite(body: string) { + afterRegisterApiCall(body) + res.send(body) + } + + // continue with default kratos handler + return createApiHandler({ + apiBaseUrlOverride: KRATOS_HOST, + forceCookieSecure: true, + forceCookieDomain: COOKIE_DOMAIN, + // @ts-expect-error missing the correct type + })(req, { ...res, send: sendOverwrite }) +} diff --git a/apps/web/src/pages/api/.ory/mail-templates/[[...templateSlug]].ts b/apps/web/src/pages/api/.ory/mail-templates/[[...templateSlug]].ts index 5818f80e5f..d1e6a68c66 100644 --- a/apps/web/src/pages/api/.ory/mail-templates/[[...templateSlug]].ts +++ b/apps/web/src/pages/api/.ory/mail-templates/[[...templateSlug]].ts @@ -1,9 +1,6 @@ import { NextApiRequest, NextApiResponse } from 'next' -import { - langTemplatesSelector, - createLangTemplates, -} from '@/helper/kratos-mail-templates/fragments' +import { createLangTemplates } from '@/helper/kratos-mail-templates/fragments' export default function de(req: NextApiRequest, res: NextApiResponse) { if (!Array.isArray(req.query.templateSlug)) res.send('invalid template url')