From 60b610054b8a72ae066f8b8b852356b883ac7b78 Mon Sep 17 00:00:00 2001 From: rdbsolutions Date: Thu, 29 Feb 2024 14:19:35 +0000 Subject: [PATCH] changed session limit and scope of protection --- middleware.js | 37 +++++++++++++++++++++++++++++++++ notusedmiddleware.js | 13 ------------ pages/api/auth/[...nextauth].js | 5 ++++- 3 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 middleware.js delete mode 100644 notusedmiddleware.js diff --git a/middleware.js b/middleware.js new file mode 100644 index 00000000..68de514a --- /dev/null +++ b/middleware.js @@ -0,0 +1,37 @@ +// export { default } from "next-auth/middleware"; + +// export const config = { +// matcher: ["/myportal/", "/myportal/:path*"], +// }; + +// import { withAuth } from 'next-auth/middleware'; + +// export default withAuth({ +// pages: { +// signIn: '/login', +// }, +// }); + +import { withAuth } from "next-auth/middleware"; + +export default withAuth({ + callbacks: { + authorized: ({ req }) => { + // verify token and return a boolean + const sessionToken = req.cookies.get("next-auth.session-token"); + if (sessionToken) return true; + else return false; + }, + }, +}); + +export const config = { + matcher: [ + "/myportal", + "/myportal/:path*", + "/newappeal", + "/newappeal/:path*", + "/account", + "/account/:path*", + ], +}; diff --git a/notusedmiddleware.js b/notusedmiddleware.js deleted file mode 100644 index 3a6a684c..00000000 --- a/notusedmiddleware.js +++ /dev/null @@ -1,13 +0,0 @@ -// export { default } from "next-auth/middleware"; - -// export const config = { -// matcher: ["/myportal/", "/myportal/:path*"], -// }; - -// import { withAuth } from 'next-auth/middleware'; - -// export default withAuth({ -// pages: { -// signIn: '/login', -// }, -// }); diff --git a/pages/api/auth/[...nextauth].js b/pages/api/auth/[...nextauth].js index 40bc0fdc..51ad0b9c 100644 --- a/pages/api/auth/[...nextauth].js +++ b/pages/api/auth/[...nextauth].js @@ -202,10 +202,13 @@ const authOptions = (locale, req, res) => { adapter: PrismaAdapter(prisma), secret: process.env.NEXTAUTH_SECRET, session: { + strategy: "database", jwt: true, pages: {}, theme: "dark", debug: true, + maxAge: 30 * 60, + updateAge: 5 * 60, // 24 hours }, cookies: { callbackUrl: { @@ -227,7 +230,7 @@ const authOptions = (locale, req, res) => { }, callbacks: { session: async (session, user) => { - //console.log(user); + console.log("callback in auth", session, user); return Promise.resolve(session); }, // redirect({ url, baseUrl }) {