feat: SW-162 Updated redirect to rewrite reducing unnecessary redirects for user
This commit is contained in:
@@ -3,7 +3,7 @@ import { cookies } from "next/headers"
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
import { authRequired, mfaRequired } from "@/constants/routes/authRequired"
|
||||
import { login, mfaLogin } from "@/constants/routes/handleAuth"
|
||||
import { login } from "@/constants/routes/handleAuth"
|
||||
import { env } from "@/env/server"
|
||||
import { internalServerError } from "@/server/errors/next"
|
||||
|
||||
@@ -77,25 +77,21 @@ export const middleware = auth(async (request) => {
|
||||
return true
|
||||
}
|
||||
}
|
||||
const isMFAPath = mfaRequired.includes(nextUrl.pathname)
|
||||
const mfaInvalid = isMFAPath ? await isMFAInvalid() : false
|
||||
|
||||
if (isLoggedIn && mfaInvalid) {
|
||||
const headers = new Headers(request.headers)
|
||||
headers.set("mfa-login", "true")
|
||||
headers.set("x-returnurl", request.nextUrl.href)
|
||||
return NextResponse.rewrite(new URL(`/${lang}/login`, request.nextUrl), {
|
||||
request: {
|
||||
headers,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
if (isLoggedIn && !hasError) {
|
||||
const isMFAPath = mfaRequired.includes(nextUrl.pathname)
|
||||
const mfaInvalid = isMFAPath ? await isMFAInvalid() : false
|
||||
if (mfaInvalid) {
|
||||
const mfaLoginUrl = mfaLogin[lang]
|
||||
const nextUrlClone = nextUrl.clone()
|
||||
nextUrlClone.host = publicUrl.host
|
||||
nextUrlClone.hostname = publicUrl.hostname
|
||||
const headers = new Headers()
|
||||
headers.append(
|
||||
"set-cookie",
|
||||
`redirectTo=${encodeURIComponent(nextUrlClone.href)}; Path=/; HttpOnly; SameSite=Lax`
|
||||
)
|
||||
return NextResponse.redirect(new URL(mfaLoginUrl, nextUrlClone), {
|
||||
headers,
|
||||
})
|
||||
}
|
||||
|
||||
const headers = new Headers(request.headers)
|
||||
headers.set("x-continue", "1")
|
||||
return NextResponse.next({
|
||||
|
||||
Reference in New Issue
Block a user