feat(SW-162): Used token instead of cookie

This commit is contained in:
Hrishikesh Vaipurkar
2024-08-09 17:45:29 +02:00
parent e7f7fb286e
commit 51df6bfd34
5 changed files with 44 additions and 70 deletions

View File

@@ -1,4 +1,3 @@
import { cookies } from "next/headers"
import { NextResponse } from "next/server"
import { authRequired, mfaRequired } from "@/constants/routes/authRequired"
@@ -55,13 +54,15 @@ export const middleware = auth(async (request) => {
nextUrlClone.hostname = publicUrl.hostname
/**
* Function to validate MFA cookie expiry
* Function to validate MFA from token data
* @returns boolean
*/
function isMFAInvalid() {
const isMFAPath = mfaRequired.includes(nextUrl.pathname)
const cookieStore = cookies()
return isMFAPath && !cookieStore.get("_MFA-validated-cookie")?.value
const isMFATokenValid = request.auth
? request.auth.token.mfa_expires_at > Date.now()
: false
return isMFAPath && !(request.auth?.token.mfa_scope && isMFATokenValid)
}
if (isLoggedIn && isMFAInvalid()) {