From 71de5ce783978954a93371bf8041497ac11d2346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa?= Date: Thu, 17 Oct 2024 17:21:32 +0200 Subject: [PATCH] types fix --- .../src/app/api/auth/[...nextauth]/options.ts | 43 ++++++++++--------- .../src/components/session-checker/index.tsx | 39 ++++++++--------- client/src/types/next-auth.d.ts | 14 +++--- 3 files changed, 48 insertions(+), 48 deletions(-) diff --git a/client/src/app/api/auth/[...nextauth]/options.ts b/client/src/app/api/auth/[...nextauth]/options.ts index 530e0d6..3e6136a 100644 --- a/client/src/app/api/auth/[...nextauth]/options.ts +++ b/client/src/app/api/auth/[...nextauth]/options.ts @@ -48,34 +48,35 @@ export const authOptions: AuthOptions = { return p; } }, {}); - const decoded = jwtDecode(token.apiToken); - const sessionToken = session.sessionToken ?? token; + const decoded = jwtDecode(token.apiToken); - if (sessionToken) { - const now = new Date().getTime(); - const exp = (decoded.iat * 1000) + (SESSION_MAX_AGE * 1000); + const sessionToken = session.sessionToken ?? token; - if (now < exp) { - return ({ - ...session, - user: sanitizedToken, - apiToken: token.apiToken, - }); - } else { - return ({ - ...session, - user: sanitizedToken, - error: "ExpiredTokenError", - }); - } - } + if (sessionToken && decoded.iat) { + const now = new Date().getTime(); + const exp = decoded.iat * 1000 + SESSION_MAX_AGE * 1000; - return ({ + if (now < exp) { + return { ...session, user: sanitizedToken, apiToken: token.apiToken, - }) + }; + } else { + return { + ...session, + user: sanitizedToken, + error: "ExpiredTokenError", + }; + } + } + + return { + ...session, + user: sanitizedToken, + apiToken: token.apiToken, + }; }, async jwt({ token, user }) { if (typeof user !== "undefined") { diff --git a/client/src/components/session-checker/index.tsx b/client/src/components/session-checker/index.tsx index 2b08b9a..91229d8 100644 --- a/client/src/components/session-checker/index.tsx +++ b/client/src/components/session-checker/index.tsx @@ -1,27 +1,26 @@ -'use client'; +"use client"; -import {PropsWithChildren, useEffect} from "react"; -import {signOut, useSession} from "next-auth/react"; +import { PropsWithChildren, useEffect } from "react"; +import { signOut, useSession } from "next-auth/react"; import { privatePaths } from "@/middleware"; -import {usePathname} from "next/navigation"; +import { usePathname } from "next/navigation"; -export default function SessionChecker ({children}: PropsWithChildren) { - const { data: sessionData } = useSession(); - const pathname = usePathname() +export default function SessionChecker({ children }: PropsWithChildren) { + const { data: sessionData } = useSession(); + const pathname = usePathname(); - console.log(sessionData?.error) - useEffect(() => { - if (!!sessionData?.error) { - // Sign out here - signOut({ - // todo: add the callbackUrl - ...privatePaths.includes(pathname) && { callbackUrl: "/signin" }, - }); - } - }, [sessionData]); + useEffect(() => { + if (!!sessionData?.error) { + // Check if the current path is part of private paths + const isPrivatePath = privatePaths.includes(pathname); - return ( - <>{children} - ) + // Sign out user and redirect them to signin page if needed + signOut({ + callbackUrl: isPrivatePath ? "/signin" : undefined, // Only add callbackUrl for private paths + }); + } + }, [sessionData, pathname]); + + return <>{children}; } diff --git a/client/src/types/next-auth.d.ts b/client/src/types/next-auth.d.ts index 6bce498..5d600bb 100644 --- a/client/src/types/next-auth.d.ts +++ b/client/src/types/next-auth.d.ts @@ -1,5 +1,4 @@ - -import { JWT } from "next-auth/jwt" +import { JWT } from "next-auth/jwt"; import "next-auth"; declare module "next-auth" { @@ -18,13 +17,14 @@ declare module "next-auth" { apiToken: string; user: User; error: string; + sessionToken: JWT; } } - declare module "next-auth/jwt" { - interface JWT { - exp: number; - iat: number; - } + interface JWT { + exp: number; + iat: number; + apiToken: string; + } }