Skip to content

Commit

Permalink
Merge pull request #3618 from serlo/kratos-fix
Browse files Browse the repository at this point in the history
feat: call kratos api endpoint manually after registering
  • Loading branch information
elbotho authored Apr 11, 2024
2 parents f6ede9c + afe3908 commit dad2f93
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
2 changes: 1 addition & 1 deletion apps/web/src/components/pages/auth/registration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ export function Registration() {
})
.then(() => {
setIsSuccessfullySubmitted(true)
window.scrollTo({ top: 0, behavior: 'smooth' })
})
.catch(
handleFlowError(
Expand All @@ -103,6 +102,7 @@ export function Registration() {
)
.finally(() => {
nProgress.done()
window.scrollTo({ top: 0, behavior: 'smooth' })
})
}

Expand Down
65 changes: 60 additions & 5 deletions apps/web/src/pages/api/.ory/[...paths].ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createApiHandler, config } from '@ory/integrations/next-edge'
import { NextApiRequest, NextApiResponse } from 'next/types'

export { config }

Expand All @@ -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']
Expand All @@ -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<string>
): Promise<void> {
// 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 })
}
Original file line number Diff line number Diff line change
@@ -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')
Expand Down

0 comments on commit dad2f93

Please sign in to comment.