// Copyright © 2022 Ory Corp // SPDX-License-Identifier: Apache-2.0 import { defaultConfig, getUrlForFlow, isQuerySet, logger, redirectOnSoftError, RouteCreator, RouteRegistrator, } from "../pkg" import { UserAuthCard } from "@ory/elements-markup" // A simple express handler that shows the registration screen. export const createRegistrationRoute: RouteCreator = (createHelpers) => (req, res, next) => { res.locals.projectName = "Create account" const { flow, return_to, after_verification_return_to, login_challenge, organization, } = req.query const { frontend, kratosBrowserUrl, logoUrl, extraPartials } = createHelpers(req, res) const initFlowQuery = new URLSearchParams({ ...(return_to && { return_to: return_to.toString() }), ...(organization && { organization: organization.toString() }), ...(after_verification_return_to && { after_verification_return_to: after_verification_return_to.toString(), }), }) if (isQuerySet(login_challenge)) { logger.debug("login_challenge found in URL query: ", { query: req.query }) initFlowQuery.append("login_challenge", login_challenge) } else { logger.debug("no login_challenge found in URL query: ", { query: req.query, }) } const initFlowUrl = getUrlForFlow( kratosBrowserUrl, "registration", initFlowQuery, ) // The flow is used to identify the settings and registration flow and // return data like the csrf_token and so on. if (!isQuerySet(flow)) { logger.debug("No flow ID found in URL query initializing login flow", { query: req.query, }) res.redirect(303, initFlowUrl) return } frontend .getRegistrationFlow({ id: flow, cookie: req.header("Cookie") }) .then(({ data: flow }) => { // Render the data using a view (e.g. Jade Template): const initLoginQuery = new URLSearchParams({ return_to: (return_to && return_to.toString()) || flow.return_to || "", }) if (flow.oauth2_login_request?.challenge) { initLoginQuery.set( "login_challenge", flow.oauth2_login_request.challenge, ) } res.render("registration", { nodes: flow.ui.nodes, card: UserAuthCard( { flow, flowType: "registration", cardImage: logoUrl, additionalProps: { loginURL: getUrlForFlow( kratosBrowserUrl, "login", initLoginQuery, ), }, }, { locale: res.locals.lang }, ), extraPartial: extraPartials?.registration, extraContext: res.locals.extraContext, }) }) .catch(redirectOnSoftError(res, next, initFlowUrl)) } export const registerRegistrationRoute: RouteRegistrator = ( app, createHelpers = defaultConfig, ) => { app.get("/registration", createRegistrationRoute(createHelpers)) }