import Link from "next/link"; import { Configuration, PublicApi } from "@ory/kratos-client"; import { getIn } from "formik"; import config from "../../config"; import levenshtein from "fast-levenshtein"; import lcs from "../../services/longestCommonSequence"; import SelfServiceForm from "../../components/Form/SelfServiceForm"; const kratos = new PublicApi(new Configuration({ basePath: config.kratos.public })); export async function getServerSideProps(context) { const flow = context.query.flow; const redirect = encodeURIComponent(`/api/accounts/login?return_to=${context.query.return_to ?? "/"}`); if (process.env.NODE_ENV === "development") { return { props: { flow: require("../../../stubs/registration.json") } }; } // The flow is used to identify the login and registration flow and // return data like the csrf_token and so on. if (!flow || typeof flow !== "string") { // No flow ID found in URL, initializing registration flow. return { redirect: { permanent: false, destination: `${config.kratos.browser}/self-service/registration/browser?return_to=${redirect}`, }, }; } try { const { status, data } = await kratos.getSelfServiceRegistrationFlow(flow); if (status === 200) return { props: { flow: data } }; throw new Error(`Failed to retrieve flow ${flow} with code ${status}`); } catch (error) { return { redirect: { permanent: false, destination: `${config.kratos.browser}/self-service/registration/browser?return_to=${redirect}`, }, }; } } const fieldsConfig = { "traits.email": { label: "Email address", autoComplete: "email", position: 0, }, password: { label: "Password", autoComplete: "new-password", position: 1, checks: [ { label: "At least 6 characters long", validate: (values, field) => { const value = getIn(values, field); return value && value.length > 5; }, }, { label: "Significantly different from the email", validate: (values, field) => { const value = getIn(values, field); const email = getIn(values, "traits.email"); // levenshtein distance higher than 5 and longest common sequence shorter than half of the password return value && email && levenshtein.get(value, email) > 5 && lcs(value, email).length / value.length <= 0.5; }, }, ], }, csrf_token: { position: 99, }, }; export default function Registration({ flow }) { return (

Sign up for a new account

or{" "} sign in {" "} if you already have one

); }