Skip to content

Commit

Permalink
types fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mluena committed Oct 17, 2024
1 parent bc7f844 commit 71de5ce
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 48 deletions.
43 changes: 22 additions & 21 deletions client/src/app/api/auth/[...nextauth]/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
39 changes: 19 additions & 20 deletions client/src/components/session-checker/index.tsx
Original file line number Diff line number Diff line change
@@ -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}</>;
}
14 changes: 7 additions & 7 deletions client/src/types/next-auth.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

import { JWT } from "next-auth/jwt"
import { JWT } from "next-auth/jwt";
import "next-auth";

declare module "next-auth" {
Expand All @@ -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;
}
}

0 comments on commit 71de5ce

Please sign in to comment.