diff --git a/package.json b/package.json index f1d0463..ac8e1f5 100644 --- a/package.json +++ b/package.json @@ -3,9 +3,9 @@ "version": "0.10.1", "description": "Authentication and session helpers for using WorkOS & AuthKit with Next.js", "sideEffects": false, - "type": "commonjs", - "main": "./dist/cjs/index.js", - "types": "./dist/cjs/index.d.ts", + "type": "module", + "main": "./dist/esm/index.js", + "types": "./dist/esm/index.d.ts", "files": [ "dist", "src", @@ -15,7 +15,7 @@ "scripts": { "clean": "rm -rf dist", "prebuild": "npm run clean", - "build": "tsc --project tsconfig-cjs.json", + "build": "tsc --project tsconfig.json", "prepublishOnly": "npm run lint", "lint": "eslint \"src/**/*.ts*\"", "test": "echo \"Error: no test specified\" && exit 1" diff --git a/src/auth.ts b/src/auth.ts index bafffc2..4715705 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -1,3 +1,5 @@ +'use server'; + import { getAuthorizationUrl } from './get-authorization-url.js'; import { cookies } from 'next/headers'; import { cookieName } from './cookie.js'; diff --git a/src/provider.tsx b/src/authkit-provider.tsx similarity index 77% rename from src/provider.tsx rename to src/authkit-provider.tsx index 9e7a2f0..444f5b9 100644 --- a/src/provider.tsx +++ b/src/authkit-provider.tsx @@ -12,7 +12,7 @@ interface AuthKitProviderProps { onSessionExpired?: false | (() => void); } -export const AuthKitProvider = ({ children, onSessionExpired = false }: AuthKitProviderProps) => { +export const AuthKitProvider = ({ children, onSessionExpired }: AuthKitProviderProps) => { React.useEffect(() => { // Return early if the session expired checks are disabled. if (onSessionExpired === false) { @@ -38,10 +38,14 @@ export const AuthKitProvider = ({ children, onSessionExpired = false }: AuthKitP throw new Error('Session expired'); } } catch (error) { - if (onSessionExpired) { - onSessionExpired(); - } else { - window.location.reload(); + // 'Failed to fetch' is the error we are looking for if the action fails + // If any other error happens, for other reasons, we should not reload the page + if (error instanceof Error && error.message.includes('Failed to fetch')) { + if (onSessionExpired) { + onSessionExpired(); + } else { + window.location.reload(); + } } } finally { visibilityChangedCalled = false; diff --git a/src/index.ts b/src/index.ts index d2e5306..d931462 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,7 @@ import { authkitMiddleware } from './middleware.js'; import { getUser, refreshSession, getSession } from './session.js'; import { getSignInUrl, getSignUpUrl, signOut } from './auth.js'; import { Impersonation } from './impersonation.js'; -import { AuthKitProvider } from './provider.js'; +import { AuthKitProvider } from './authkit-provider.js'; export { handleAuth, diff --git a/src/session.ts b/src/session.ts index 347577c..05ec9f2 100644 --- a/src/session.ts +++ b/src/session.ts @@ -1,3 +1,5 @@ +'use server'; + import { redirect } from 'next/navigation'; import { cookies, headers } from 'next/headers'; import { NextRequest, NextResponse } from 'next/server'; diff --git a/tsconfig-cjs.json b/tsconfig-cjs.json deleted file mode 100644 index a6c629b..0000000 --- a/tsconfig-cjs.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "extends": "./tsconfig-base.json", - "compilerOptions": { - "outDir": "./dist/cjs", - "module": "CommonJS" - } -} diff --git a/tsconfig-base.json b/tsconfig.json similarity index 80% rename from tsconfig-base.json rename to tsconfig.json index 8b8d4c8..29d2c94 100644 --- a/tsconfig-base.json +++ b/tsconfig.json @@ -3,16 +3,16 @@ "target": "ES2018", "lib": ["DOM", "ESNext", "DOM.Iterable"], "jsx": "react", - "module": "ES2020", - "moduleResolution": "Node", "sourceMap": true, "declaration": true, - "outDir": "./dist", "importHelpers": true, "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "strict": true, "alwaysStrict": true, - "skipLibCheck": true + "skipLibCheck": true, + "outDir": "./dist/esm", + "module": "ES2020", + "moduleResolution": "Node" } }