fix: improve auth handling and logging

This commit is contained in:
Michael Zetterberg
2024-08-22 13:39:06 +02:00
parent 71d93864dd
commit a33a69fb58
15 changed files with 174 additions and 84 deletions

View File

@@ -49,9 +49,9 @@ export const middleware = auth(async (request) => {
}
const publicUrl = new URL(env.PUBLIC_URL)
const nextUrlClone = nextUrl.clone()
nextUrlClone.host = publicUrl.host
nextUrlClone.hostname = publicUrl.hostname
const nextUrlPublic = nextUrl.clone()
nextUrlPublic.host = publicUrl.host
nextUrlPublic.hostname = publicUrl.hostname
/**
* Function to validate MFA from token data
@@ -67,7 +67,7 @@ export const middleware = auth(async (request) => {
if (isLoggedIn && isMFAPath && isMFAInvalid()) {
const headers = new Headers(request.headers)
headers.set("x-returnurl", nextUrlClone.href)
headers.set("x-returnurl", nextUrlPublic.href)
headers.set("x-login-source", "mfa")
return NextResponse.rewrite(new URL(`/${lang}/login`, request.nextUrl), {
request: {
@@ -87,13 +87,16 @@ export const middleware = auth(async (request) => {
const headers = new Headers()
headers.append(
"set-cookie",
`redirectTo=${encodeURIComponent(nextUrlClone.href)}; Path=/; HttpOnly; SameSite=Lax`
`redirectTo=${encodeURIComponent(nextUrlPublic.href)}; Path=/; HttpOnly; SameSite=Lax`
)
const loginUrl = login[lang]
return NextResponse.redirect(new URL(loginUrl, nextUrlClone), {
const redirectUrl = new URL(loginUrl, nextUrlPublic)
const redirectOpts = {
headers,
})
}
console.log(`[authRequired] redirecting to: ${redirectUrl}`, redirectOpts)
return NextResponse.redirect(redirectUrl, redirectOpts)
}) as NextMiddleware // See comment above
export const matcher: MiddlewareMatcher = (request) => {

View File

@@ -23,6 +23,7 @@ export const middleware: NextMiddleware = (request) => {
const headers = new Headers(request.headers)
headers.set("x-returnurl", redirectTo)
headers.set("x-logout-source", "seamless")
return NextResponse.rewrite(new URL(`/${lang}/logout`, request.nextUrl), {
request: {

View File

@@ -34,7 +34,9 @@ export const middleware: NextMiddleware = async (request) => {
nextUrlClone.hostname = publicUrl.hostname
const overviewUrl = overview[lang]
return NextResponse.redirect(new URL(overviewUrl, nextUrlClone))
const redirectUrl = new URL(overviewUrl, nextUrlClone)
console.log(`[myPages] redirecting to: ${redirectUrl}`)
return NextResponse.redirect(redirectUrl)
}
const pathNameWithoutLang = nextUrl.pathname.replace(`/${lang}`, "")

View File

@@ -1,3 +1,6 @@
import { env } from "@/env/server"
import { internalServerError } from "@/server/errors/next"
import { findLang } from "@/utils/languages"
import { removeTrailingSlash } from "@/utils/url"
@@ -6,6 +9,17 @@ import type { NextRequest } from "next/server"
export function getDefaultRequestHeaders(request: NextRequest) {
const lang = findLang(request.nextUrl.pathname)!
let nextUrl
if (env.PUBLIC_URL) {
const publicUrl = new URL(env.PUBLIC_URL)
const nextUrlPublic = request.nextUrl.clone()
nextUrlPublic.host = publicUrl.host
nextUrlPublic.hostname = publicUrl.hostname
nextUrl = nextUrlPublic
} else {
nextUrl = request.nextUrl
}
const headers = new Headers(request.headers)
headers.set("x-lang", lang)
headers.set(
@@ -14,7 +28,7 @@ export function getDefaultRequestHeaders(request: NextRequest) {
request.nextUrl.pathname.replace(`/${lang}`, "").replace(`/webview`, "")
)
)
headers.set("x-url", removeTrailingSlash(request.nextUrl.href))
headers.set("x-url", removeTrailingSlash(nextUrl.href))
return headers
}