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
This commit is contained in:
Joakim Jäderberg
2025-02-12 09:42:15 +00:00
parent bcfa84324f
commit 38da263cb3
2 changed files with 34 additions and 13 deletions

View File

@@ -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<Redirect>
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<void>
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)
})
}

View File

@@ -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<OnSubmitHandler> {
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",
}
}
}