Chore/SW-2878 extract booking confirmation pag * chore(SW-2878): Moved booking confirmation page to booking-flow package * chore(SW-2878): Fixed promo styles as per design * chore(SW-2878): Kept tiny duplicate function to avoid export from booking-flow package Approved-by: Anton Gunnarsson
52 lines
1.7 KiB
TypeScript
52 lines
1.7 KiB
TypeScript
import { type NextMiddleware, NextResponse } from "next/server"
|
|
|
|
import { SEARCHTYPE } from "@scandic-hotels/common/constants/booking"
|
|
import { login } from "@scandic-hotels/common/constants/routes/handleAuth"
|
|
import { findLang } from "@scandic-hotels/common/utils/languages"
|
|
import { SEARCH_TYPE_REDEMPTION } from "@scandic-hotels/trpc/constants/booking"
|
|
import { isValidSession } from "@scandic-hotels/trpc/utils/session"
|
|
|
|
import { getPublicNextURL } from "@/server/utils"
|
|
|
|
import { auth } from "@/auth"
|
|
|
|
import { getDefaultRequestHeaders } from "./utils"
|
|
|
|
import type { MiddlewareMatcher } from "@/types/middleware"
|
|
|
|
export const middleware: NextMiddleware = async (request) => {
|
|
// Redirect user to login if reward nights search and not logged in
|
|
const isRedemption =
|
|
request.nextUrl.searchParams.get(SEARCHTYPE) === SEARCH_TYPE_REDEMPTION
|
|
const session = await auth() // Check for user session
|
|
if (isRedemption && !isValidSession(session)) {
|
|
const lang = findLang(request.nextUrl.pathname)!
|
|
const nextUrlPublic = getPublicNextURL(request)
|
|
const headers = new Headers()
|
|
headers.append(
|
|
"set-cookie",
|
|
`redirectTo=${encodeURIComponent(nextUrlPublic.href)}; Path=/; HttpOnly; SameSite=Lax`
|
|
)
|
|
|
|
const loginUrl = login[lang]
|
|
const redirectUrl = new URL(loginUrl, nextUrlPublic)
|
|
const redirectOpts = {
|
|
headers,
|
|
}
|
|
return NextResponse.redirect(redirectUrl, redirectOpts)
|
|
}
|
|
|
|
const headers = getDefaultRequestHeaders(request)
|
|
return NextResponse.next({
|
|
request: {
|
|
headers,
|
|
},
|
|
})
|
|
}
|
|
|
|
export const matcher: MiddlewareMatcher = (request) => {
|
|
return !!request.nextUrl.pathname.match(
|
|
/^\/(da|de|en|fi|no|sv)\/(hotelreservation)/
|
|
)
|
|
}
|