Merged in feat/SW-1488-unlink-sas-account (pull request #1349)

Implement unlink SAS flow

Approved-by: Joakim Jäderberg
This commit is contained in:
Anton Gunnarsson
2025-02-20 15:09:06 +00:00
parent fc720b1dbc
commit 340f6d1714
22 changed files with 216 additions and 82 deletions

View File

@@ -2,6 +2,7 @@ import { cookies } from "next/headers"
import { redirect } from "next/navigation"
import { z } from "zod"
import { myPages } from "@/constants/routes/myPages"
import { serverClient } from "@/lib/trpc/server"
import { getIntl } from "@/i18n"
@@ -12,17 +13,21 @@ import OneTimePasswordForm, {
type OnSubmitHandler,
} from "./OneTimePasswordForm"
import type { ReactNode } from "react"
import type { LangParams, PageArgs, SearchParams } from "@/types/params"
import type { Lang } from "@/constants/languages"
const otpError = z.enum(["invalidCode", "expiredCode"])
const intent = z.enum(["link", "unlink"])
const searchParamsSchema = z.object({
intent: z.enum(["link"]),
intent: intent,
to: z.string(),
error: otpError.optional(),
})
export type OtpError = z.infer<typeof otpError>
type Intent = z.infer<typeof intent>
export default async function SASxScandicOneTimePasswordPage({
searchParams,
@@ -88,26 +93,37 @@ export default async function SASxScandicOneTimePasswordPage({
switch (intent) {
case "link":
return handleLinkAccount({ lang: params.lang })
case "unlink":
return handleUnlinkAccount({ lang: params.lang })
}
}
const maskedContactInfo = () => (
<>
<br />
<strong>{to}</strong>
<br />
</>
)
const intentDescriptions: Record<Intent, ReactNode> = {
link: intl.formatMessage<React.ReactNode>(
{
id: "Please enter the code sent to <maskedContactInfo></maskedContactInfo> in order to confirm your account linking.",
},
{ maskedContactInfo }
),
unlink: intl.formatMessage<React.ReactNode>(
{
id: "Please enter the code sent to <maskedContactInfo></maskedContactInfo> in order to unlink your accounts.",
},
{ maskedContactInfo }
),
}
return (
<OneTimePasswordForm
heading={intl.formatMessage({ id: "Verification code" })}
ingress={intl.formatMessage<React.ReactNode>(
{
id: "Please enter the code sent to <maskedContactInfo></maskedContactInfo> in order to confirm your account linking.",
},
{
maskedContactInfo: () => (
<>
<br />
<strong>{to}</strong>
<br />
</>
),
}
)}
ingress={intentDescriptions[intent]}
footnote={intl.formatMessage({
id: "This verifcation is needed for additional security.",
})}
@@ -168,3 +184,34 @@ async function handleLinkAccount({
}
}
}
async function handleUnlinkAccount({
lang,
}: {
lang: Lang
}): ReturnType<OnSubmitHandler> {
const [res, error] = await safeTry(serverClient().partner.sas.unlinkAccount())
if (!res || error) {
console.error("[SAS] unlink account error", error)
return {
url: `/${lang}/sas-x-scandic/error`,
}
}
switch (res.linkingState) {
case "unlinked":
return {
url: `/${lang}/sas-x-scandic/unlink/success`,
type: "replace",
}
case "notLinked":
return {
url: myPages[lang],
}
case "error":
return {
url: `/${lang}/sas-x-scandic/error`,
type: "replace",
}
}
}