fix: adjust session token refresh and caching intervals to 1 minute for improved performance

This commit is contained in:
swve 2025-08-09 10:10:52 +02:00
parent c194114bfc
commit 887046203e
2 changed files with 6 additions and 6 deletions

View file

@ -91,12 +91,12 @@ export const nextAuthOptions = {
token.user = userFromOAuth.data; token.user = userFromOAuth.data;
} }
// Refresh token only if it's close to expiring (5 minutes before expiry) // Refresh token only if it's close to expiring (1 minute before expiry)
if (token?.user?.tokens) { if (token?.user?.tokens) {
const tokenExpiry = token.user.tokens.expiry || 0; const tokenExpiry = token.user.tokens.expiry || 0;
const fiveMinutes = 5 * 60 * 1000; const oneMinute = 1 * 60 * 1000;
if (Date.now() + fiveMinutes >= tokenExpiry) { if (Date.now() + oneMinute >= tokenExpiry) {
const RefreshedToken = await getNewAccessTokenUsingRefreshTokenServer( const RefreshedToken = await getNewAccessTokenUsingRefreshTokenServer(
token?.user?.tokens?.refresh_token token?.user?.tokens?.refresh_token
); );
@ -118,11 +118,11 @@ export const nextAuthOptions = {
async session({ session, token }: any) { async session({ session, token }: any) {
// Include user information in the session // Include user information in the session
if (token.user) { if (token.user) {
// Cache the session for 5 minutes to avoid frequent API calls // Cache the session for 1 minute to refresh every minute
const cacheKey = `user_session_${token.user.tokens.access_token}`; const cacheKey = `user_session_${token.user.tokens.access_token}`;
let cachedSession = global.sessionCache?.[cacheKey]; let cachedSession = global.sessionCache?.[cacheKey];
if (cachedSession && Date.now() - cachedSession.timestamp < 5 * 60 * 1000) { if (cachedSession && Date.now() - cachedSession.timestamp < 1 * 60 * 1000) {
return cachedSession.data; return cachedSession.data;
} }

View file

@ -22,7 +22,7 @@ export default function RootLayout({
<head /> <head />
<body> <body>
{isDevEnv ? '' : <Script data-website-id="a1af6d7a-9286-4a1f-8385-ddad2a29fcbb" src="/umami/script.js" />} {isDevEnv ? '' : <Script data-website-id="a1af6d7a-9286-4a1f-8385-ddad2a29fcbb" src="/umami/script.js" />}
<SessionProvider key="session-provider"> <SessionProvider key="session-provider" refetchInterval={60000}>
<LHSessionProvider> <LHSessionProvider>
<StyledComponentsRegistry> <StyledComponentsRegistry>
<motion.main <motion.main