I am building a project using Next js 14, so I have an app/
directory and don’t have a pages/
directory.
I am also using next-auth v5 beta
for authentication and also this package called idle-session-timeout
from npm.
I added this code in my layout.tsx
of my homepage:
import { IdleSessionTimeout } from "idle-session-timeout";
import { auth, signOut } from "../api/auth/[...nextauth]/auth";
// some code here
const session = await auth();
let sessionTimeout = new IdleSessionTimeout(5 * 60 * 1000);
sessionTimeout.onTimeOut = () => {
// here you can call your server to log out the user
console.log("timeOut");
};
sessionTimeout.start();
// rest of the code here
I get the following error (also shown in image):
Error: window is not defined
I believe the error is because this is a server component, but I cannot change is to client component because I need to use await auth()
.
Please help me out.