From 38da263cb3d4b6c8d8824bd072c3a42e3b672116 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20J=C3=A4derberg?= Date: Wed, 12 Feb 2025 09:42:15 +0000 Subject: [PATCH] Merged in fix/sas-link (pull request #1317) SAS Handle redirects from server actions on the client * fix: return values instead of doing redirect in server action * Merge branch 'master' of bitbucket.org:scandic-swap/web into fix/sas-link * fix: SAS use client side redirects since netlify doesn't seem to support redirects inside server actions Approved-by: Linus Flood --- .../sas-x-scandic/otp/OneTimePasswordForm.tsx | 13 +++++-- .../(protected)/sas-x-scandic/otp/page.tsx | 34 +++++++++++++------ 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/app/[lang]/(partner)/(sas)/(protected)/sas-x-scandic/otp/OneTimePasswordForm.tsx b/app/[lang]/(partner)/(sas)/(protected)/sas-x-scandic/otp/OneTimePasswordForm.tsx index 835e87546..c9b05b960 100644 --- a/app/[lang]/(partner)/(sas)/(protected)/sas-x-scandic/otp/OneTimePasswordForm.tsx +++ b/app/[lang]/(partner)/(sas)/(protected)/sas-x-scandic/otp/OneTimePasswordForm.tsx @@ -24,6 +24,9 @@ import styles from "./OneTimePasswordForm.module.css" import type { RequestOtpError } from "@/server/routers/partners/sas/otp/request/requestOtpError" import type { OtpError } from "./page" +type Redirect = { url: string; type?: "replace" | "push" } +export type OnSubmitHandler = (args: { otp: string }) => Promise + export default function OneTimePasswordForm({ heading, ingress, @@ -36,7 +39,7 @@ export default function OneTimePasswordForm({ ingress: string | ReactNode footnote?: string | ReactNode otpLength: number - onSubmit: (args: { otp: string }) => Promise + onSubmit: OnSubmitHandler error?: OtpError }) { const router = useRouter() @@ -96,7 +99,13 @@ export default function OneTimePasswordForm({ function handleOTPEntered(otp: string) { startTransition(async () => { - await onSubmit({ otp }) + const redirectRes = await onSubmit({ otp }) + if (redirectRes.type === "replace") { + router.replace(redirectRes.url) + return + } + + router.push(redirectRes.url) }) } diff --git a/app/[lang]/(partner)/(sas)/(protected)/sas-x-scandic/otp/page.tsx b/app/[lang]/(partner)/(sas)/(protected)/sas-x-scandic/otp/page.tsx index b03dfd0a1..b6f2453d2 100644 --- a/app/[lang]/(partner)/(sas)/(protected)/sas-x-scandic/otp/page.tsx +++ b/app/[lang]/(partner)/(sas)/(protected)/sas-x-scandic/otp/page.tsx @@ -8,7 +8,7 @@ import { getIntl } from "@/i18n" import { safeTry } from "@/utils/safeTry" import { SAS_TOKEN_STORAGE_KEY } from "../sasUtils" -import OneTimePasswordForm from "./OneTimePasswordForm" +import OneTimePasswordForm, {type OnSubmitHandler } from "./OneTimePasswordForm" import type { LangParams, PageArgs, SearchParams } from "@/types/params" import type { Lang } from "@/constants/languages" @@ -40,7 +40,7 @@ export default async function SASxScandicOneTimePasswordPage({ redirect(`/${params.lang}/sas-x-scandic/login?intent=${intent}`) } - async function handleOtpVerified({ otp }: { otp: string }) { + const handleOtpVerified: OnSubmitHandler = async ({ otp }) => { "use server" const [data, error] = await safeTry( serverClient().partner.sas.verifyOtp({ otp }) @@ -52,16 +52,18 @@ export default async function SASxScandicOneTimePasswordPage({ switch (data.status) { case "ABUSED": - redirect( - `/${params.lang}/sas-x-scandic/error?errorCode=tooManyFailedAttempts` - ) + return { + url: `/${params.lang}/sas-x-scandic/error?errorCode=tooManyFailedAttempts`, + } case "EXPIRED": { const search = new URLSearchParams({ ...searchParams, error: "expiredCode", }).toString() - redirect(`/${params.lang}/sas-x-scandic/otp?${search}`) + return { + url: `/${params.lang}/sas-x-scandic/otp?${search}`, + } } case "RETRY": { const search = new URLSearchParams({ @@ -69,7 +71,9 @@ export default async function SASxScandicOneTimePasswordPage({ error: "invalidCode", }).toString() - redirect(`/${params.lang}/sas-x-scandic/otp?${search}`) + return { + url: `/${params.lang}/sas-x-scandic/otp?${search}`, + } } case "NOTSENT": case "NULL": @@ -126,17 +130,25 @@ function verifyTokenValidity(token: string | undefined) { } } -async function handleLinkAccount({ lang }: { lang: Lang }) { +async function handleLinkAccount({ + lang, +}: { + lang: Lang +}): ReturnType { const [res, error] = await safeTry(serverClient().partner.sas.linkAccount()) if (!res || error) { console.error("[SAS] link account error", error) - redirect(`/${lang}/sas-x-scandic/error?errorCode=link_error`) + return { + url: `/${lang}/sas-x-scandic/error?errorCode=link_error`, + } } console.log("[SAS] link account response", res) switch (res.linkingState) { case "linked": - redirect(`/${lang}/sas-x-scandic/link/success`, RedirectType.replace) - break + return { + url: `/${lang}/sas-x-scandic/link/success`, + type: "replace", + } } }