|
| 1 | +import { NextPage } from "next"; |
| 2 | +import React, { useCallback, useEffect, useState } from "react"; |
| 3 | +import { |
| 4 | + UiNode, |
| 5 | + UiNodeInputAttributes, |
| 6 | + UpdateVerificationFlowBody, |
| 7 | + UpdateVerificationFlowWithCodeMethod, |
| 8 | + VerificationFlow, |
| 9 | +} from "@ory/client"; |
| 10 | +import { NextRouter, useRouter } from "next/router"; |
| 11 | +import { handleFlowError } from "../util/handleFlowError"; |
| 12 | +import { kratos } from "../api/kratos"; |
| 13 | +import { Flow } from "../components/Flow"; |
| 14 | +import PageLayout from "../components/PageLayout"; |
| 15 | +import { Spinner } from "@canonical/react-components"; |
| 16 | +import { AxiosError } from "axios"; |
| 17 | + |
| 18 | +function setFlowIDQueryParam(router: NextRouter, flowId: string) { |
| 19 | + void router.push( |
| 20 | + { |
| 21 | + pathname: router.pathname, |
| 22 | + query: { ...router.query, flow: flowId }, |
| 23 | + }, |
| 24 | + undefined, |
| 25 | + { shallow: true }, |
| 26 | + ); |
| 27 | +} |
| 28 | + |
| 29 | +const Verification: NextPage = () => { |
| 30 | + const [flow, setFlow] = useState<VerificationFlow>(); |
| 31 | + const router = useRouter(); |
| 32 | + const { |
| 33 | + return_to: returnTo, |
| 34 | + flow: flowId, |
| 35 | + code: verificationCode, |
| 36 | + } = router.query; |
| 37 | + |
| 38 | + const redirectToErrorPage = () => { |
| 39 | + const idParam = flowId ? `?id=${flowId.toString()}` : ""; |
| 40 | + window.location.href = `./error${idParam}`; |
| 41 | + }; |
| 42 | + |
| 43 | + useEffect(() => { |
| 44 | + if (!router.isReady || flow) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + if (flowId) { |
| 49 | + kratos |
| 50 | + .getVerificationFlow({ id: String(flowId) }) |
| 51 | + .then(({ data }) => { |
| 52 | + if (verificationCode) { |
| 53 | + const predicate = (node: UiNode) => |
| 54 | + node.group === "code" && |
| 55 | + node.type === "input" && |
| 56 | + (node.attributes as UiNodeInputAttributes).name === "code"; |
| 57 | + const codeUiNode = data.ui.nodes.find(predicate); |
| 58 | + if (codeUiNode) { |
| 59 | + (codeUiNode.attributes as UiNodeInputAttributes).value = |
| 60 | + String(verificationCode); |
| 61 | + } |
| 62 | + } |
| 63 | + setFlowIDQueryParam(router, data.id); |
| 64 | + setFlow(data); |
| 65 | + }) |
| 66 | + .catch(handleFlowError("verification", setFlow)) |
| 67 | + .catch(redirectToErrorPage); |
| 68 | + |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + kratos |
| 73 | + .createBrowserVerificationFlow({ |
| 74 | + returnTo: returnTo ? String(returnTo) : undefined, |
| 75 | + }) |
| 76 | + .then(({ data }) => { |
| 77 | + setFlow(data); |
| 78 | + setFlowIDQueryParam(router, String(data.id)); |
| 79 | + }) |
| 80 | + .catch(handleFlowError("verification", setFlow)) |
| 81 | + .catch(redirectToErrorPage); |
| 82 | + }, [flowId, router, router.isReady, returnTo]); |
| 83 | + |
| 84 | + const handleSubmit = useCallback( |
| 85 | + (values: UpdateVerificationFlowBody) => { |
| 86 | + return kratos |
| 87 | + .updateVerificationFlow({ |
| 88 | + flow: String(flow?.id), |
| 89 | + updateVerificationFlowBody: { |
| 90 | + ...(values as UpdateVerificationFlowWithCodeMethod), |
| 91 | + method: "code", |
| 92 | + }, |
| 93 | + }) |
| 94 | + .then(({ data }) => { |
| 95 | + if ("continue_with" in data) { |
| 96 | + const continue_with: any = ( |
| 97 | + data as { continue_with: Array<{ action: string } & any> } |
| 98 | + ).continue_with[0]; |
| 99 | + if (continue_with.action === "redirect_browser_to") { |
| 100 | + window.location.href = continue_with.redirect_browser_to; |
| 101 | + } |
| 102 | + return; |
| 103 | + } |
| 104 | + setFlow(data); |
| 105 | + }) |
| 106 | + .catch(handleFlowError("verification", setFlow)) |
| 107 | + .catch((err: AxiosError<VerificationFlow>) => { |
| 108 | + if (err.response?.status === 400) { |
| 109 | + setFlow(err.response.data); |
| 110 | + return; |
| 111 | + } |
| 112 | + return Promise.reject(err); |
| 113 | + }); |
| 114 | + }, |
| 115 | + [flow], |
| 116 | + ); |
| 117 | + |
| 118 | + if (!flow) { |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + return ( |
| 123 | + <PageLayout title="Verify your email"> |
| 124 | + {flow ? <Flow onSubmit={handleSubmit} flow={flow} /> : <Spinner />} |
| 125 | + </PageLayout> |
| 126 | + ); |
| 127 | +}; |
| 128 | + |
| 129 | +export default Verification; |
0 commit comments