From 6417118a23acd27327abbdaa4d353dbaf35b2a7a Mon Sep 17 00:00:00 2001
From: Timm Fitschen <t.fitschen@indiscale.com>
Date: Mon, 18 Mar 2024 08:40:02 +0100
Subject: [PATCH] WIP: OAuth authn

---
 pages/_app.tsx                  | 2 +-
 pages/api/auth/[...nextauth].ts | 5 +++--
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/pages/_app.tsx b/pages/_app.tsx
index 640eb28..c9bf43c 100644
--- a/pages/_app.tsx
+++ b/pages/_app.tsx
@@ -208,7 +208,7 @@ function MyApp ({
 
   return (
     <SessionProvider session={session}
-            refetchInterval={3 * 60}
+            refetchInterval={10}
             refetchOnWindowFocus={true}
     >
       <App>{renderComponent()}</App>
diff --git a/pages/api/auth/[...nextauth].ts b/pages/api/auth/[...nextauth].ts
index 9007c33..8f1f781 100644
--- a/pages/api/auth/[...nextauth].ts
+++ b/pages/api/auth/[...nextauth].ts
@@ -93,6 +93,7 @@ export const authOptions = {
       // Initial sign in
       if (account && user) {
         return {
+          lastRefresh: Date.now(),
           accessToken: account.access_token,
           accessTokenExpires: account.expires_at,
           refreshToken: account.refresh_token,
@@ -100,8 +101,8 @@ export const authOptions = {
         }
       }
 
-      // Return previous token if the access token will still not be expired in 60 seconds.
-      if (Date.now() + 60000 < token.accessTokenExpires) {
+      if (token.lastRefresh + 10000 > Date.now()) {
+        // return if last refresh is 10 seconds or younger
         return token
       }
 
-- 
GitLab