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

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