fix(WEB-132): absolute seamless login return urls

This commit is contained in:
Michael Zetterberg
2024-04-25 08:54:05 +02:00
parent 1ec86f43ff
commit bcbc829e2e
5 changed files with 60 additions and 23 deletions

View File

@@ -1,10 +1,11 @@
import { NextResponse } from "next/server"
import { auth } from "@/auth"
import { findLang } from "@/constants/languages"
import { authRequired } from "@/constants/routes/authRequired"
import { login } from "@/constants/routes/handleAuth"
import { auth } from "@/auth"
import type { NextMiddleware } from "next/server"
import type { MiddlewareMatcher } from "@/types/middleware"
@@ -44,8 +45,15 @@ export const middleware = auth(async (request) => {
return NextResponse.next()
}
const headers = new Headers()
headers.append(
"set-cookie",
`redirectTo=${encodeURIComponent(nextUrl.href)}; Path=/; HttpOnly; SameSite=Lax`
)
const loginUrl = login[lang]
return NextResponse.redirect(new URL(loginUrl, request.nextUrl))
return NextResponse.redirect(new URL(loginUrl, request.nextUrl), {
headers,
})
}) as NextMiddleware // See comment above
export const matcher: MiddlewareMatcher = (request) => {