fix: set cookie in header on rewrite

This commit is contained in:
Christel Westerberg
2024-05-07 12:10:25 +02:00
parent b58b5f368d
commit 777fd1e5b6
4 changed files with 38 additions and 12 deletions

View File

@@ -10,8 +10,6 @@ import { serverClient } from "@/lib/trpc/server"
import MaxWidth from "@/components/MaxWidth" import MaxWidth from "@/components/MaxWidth"
import Content from "@/components/MyPages/AccountPage/Webview/Content" import Content from "@/components/MyPages/AccountPage/Webview/Content"
import Link from "@/components/TempDesignSystem/Link"
import Title from "@/components/Title"
import styles from "./page.module.css" import styles from "./page.module.css"
@@ -39,6 +37,18 @@ export default async function MyPages({
return notFound() return notFound()
} }
// Check if the access token is valid. If not, redirect to the refresh page.
await serverClient({
onError(opts) {
const returnUrl = new URLSearchParams({
returnurl: `${params.lang}/webview/${searchParams.uri}`,
})
const refreshUrl = `/${params.lang}/webview/refresh?${returnUrl.toString()}`
redirect(refreshUrl)
},
}).user.get()
const accountPage = await serverClient({ const accountPage = await serverClient({
onError() { onError() {
const returnUrl = new URLSearchParams({ const returnUrl = new URLSearchParams({
@@ -57,10 +67,6 @@ export default async function MyPages({
return ( return (
<MaxWidth className={styles.blocks} tag="main"> <MaxWidth className={styles.blocks} tag="main">
<header>
<Title as="h2">{_("Welcome")}</Title>
<Link href={link.href}>{link.title}</Link>
</header>
<Content lang={params.lang} content={accountPage.content} /> <Content lang={params.lang} content={accountPage.content} />
</MaxWidth> </MaxWidth>
) )

View File

@@ -49,12 +49,12 @@ export const points = {
/** @type {import('@/types/routes').LangRoute} */ /** @type {import('@/types/routes').LangRoute} */
export const programOverview = { export const programOverview = {
da: `/da/webview/ophold`, da: `/da/webview/about-scandic-friends`,
de: `/de/webview/aufenthalte`, de: `/de/webview/about-scandic-friends`,
en: `/en/webview/stays`, en: `/en/webview/about-scandic-friends`,
fi: `/fi/webview/oleskeluni`, fi: `/fi/webview/about-scandic-friends`,
no: `/no/webview/opphold`, no: `/no/webview/om-scandic-friends`,
sv: `/sv/webview/vistelser`, sv: `/sv/webview/om-scandic-friends`,
} }
export const webviews = [ export const webviews = [

View File

@@ -1,3 +1,4 @@
import { notFound } from "next/navigation"
import { type NextMiddleware, NextResponse } from "next/server" import { type NextMiddleware, NextResponse } from "next/server"
import { findLang } from "@/constants/languages" import { findLang } from "@/constants/languages"
@@ -36,6 +37,8 @@ export const middleware: NextMiddleware = async (request) => {
nextUrl nextUrl
) )
) )
} else {
return notFound()
} }
} }
@@ -69,6 +72,7 @@ export const middleware: NextMiddleware = async (request) => {
{ {
headers: { headers: {
"Set-Cookie": `webviewToken=${decryptedData}; Secure; HttpOnly; Path=/; SameSite=Strict;`, "Set-Cookie": `webviewToken=${decryptedData}; Secure; HttpOnly; Path=/; SameSite=Strict;`,
Cookie: `webviewToken=${decryptedData}`,
}, },
} }
) )
@@ -81,6 +85,7 @@ export const middleware: NextMiddleware = async (request) => {
{ {
headers: { headers: {
"Set-Cookie": `webviewToken=${decryptedData}; Secure; HttpOnly; Path=/; SameSite=Strict;`, "Set-Cookie": `webviewToken=${decryptedData}; Secure; HttpOnly; Path=/; SameSite=Strict;`,
Cookie: `webviewToken=${decryptedData}`,
}, },
} }
) )

View File

@@ -33,6 +33,21 @@ export function createContextInner(opts: CreateContextOptions) {
export function createContext() { export function createContext() {
const h = headers() const h = headers()
// const cookie = cookies()
// const webviewTokenCookie = cookie.get("webviewToken")
// if (webviewTokenCookie) {
// // since the token exists, this is a subsequent visit
// // we're done, allow it
// return createContextInner({
// session: {
// token: { access_token: webviewTokenCookie.value },
// },
// })
// }
// const session = await auth()
return createContextInner({ return createContextInner({
auth, auth,
lang: h.get("x-lang") as Lang, lang: h.get("x-lang") as Lang,