Comments (9)
I having the same problem. I think that problem is very serious, is there any progress??😢
from kinde-auth-nextjs.
Here's a snippet from my custom TS implementation of the AuthProvider
that supports token refresh:
// Auto-refresh token before it expires
useEffect(() => {
let timeoutId: Timer;
const scheduleTokenRefresh = () => {
if (state.accessToken) {
try {
const expClaim = state.getClaim("exp", "access_token");
if (expClaim && typeof expClaim.value === "number") {
const currentTime = Math.floor(Date.now() / 1000);
const timeToExpiry = expClaim.value - currentTime;
// Refresh the token 60 seconds before it actually expires
if (timeToExpiry > 60) {
timeoutId = setTimeout(
// eslint-disable-next-line @typescript-eslint/no-misused-promises
async () => {
await checkSession();
},
(timeToExpiry - 60) * 1000,
);
}
}
} catch (error) {
console.error("Error fetching exp claim:", error);
}
}
};
void scheduleTokenRefresh();
return () => {
timeoutId && clearTimeout(timeoutId);
};
}, [state.accessToken, state.getClaim, checkSession, state]);
Note: checkSession()
is a fixed and cleaned up TS version of https://github.com/kinde-oss/kinde-auth-nextjs/blob/main/src/frontend/AuthProvider.jsx#L61
from kinde-auth-nextjs.
Here's a TS implementation of tokenFetcher() with support for redirecting the browser session to login page in case auth has expired while the page is still open:
const tokenFetcher = async (
url: string,
router: ReturnType<typeof useRouter>,
): Promise<KindeSetupResponse | undefined> => {
let response;
try {
response = await fetch(url);
} catch (e) {
throw new Error("AuthProvider: Failed to fetch auth data", {
cause: e as Error,
});
}
if (response.ok) {
return await response.json();
} else {
if (response.status === 401) {
const loginUrl = response.headers.get("location") || LOGIN_PAGE;
logger.debug(
"AuthProvider: Auth token expired, redirecting to login page",
{ location: loginUrl },
);
router.push(loginUrl);
} else {
logger.error(
"AuthProvider: Unknown error while trying to obtain auth data",
{
response: {
status: response.status,
statusText: response.statusText,
},
},
);
}
}
};
from kinde-auth-nextjs.
Extremely sorry for letting this one slip through - I'll be better at addressing these issues.
I'm assuming this is for Pages router?
I'll prioritise this!
from kinde-auth-nextjs.
Thank you! I think this problem is also happen in app router.
from kinde-auth-nextjs.
Hey @tak0m0 would you be able to share any extra details or code snippets to help me reproduce this error in the App Router w/ useKindeBrowserClient
from kinde-auth-nextjs.
I’m using “useKindeBrowserClient” in the header and user menu, but it doesn’t handle expired sessions and re-authentication. It often returns a 401 error. Many authentication libraries try to authenticate again with a refresh token even if the main token has expired, but this does not appear to be possible with this library.
I set a 1,296,000 seconds refresh token expiry but realized that it expires earlier than that time.
from kinde-auth-nextjs.
Hey guys, I made a release 2.3.7-beta.20 which should fix the problems. It's still in beta because it needs some testing.
from kinde-auth-nextjs.
Related Issues (20)
- Bug: module augmentation fails due to how library publishes types
- Bug: Error: Cannot get user details, no authentication credential found HOT 3
- Bug: user cookie is not set properly on callback, causing error on middleware HOT 7
- Bug: middleware with withAuth does not work: TypeError: Cannot read properties of undefined (reading 'value') HOT 2
- refreshTokens in Next.js results in ReferenceError: config is not defined HOT 2
- Bug: TypeError: (0 , react__WEBPACK_IMPORTED_MODULE_0__.createContext) is not a function HOT 2
- Bug: Error 500 on production HOT 8
- Bug: `/api/auth/setup` is failing with 401 Unauthorized HOT 7
- Bug: 405 method not allowed - Middleware + handler don't handle non-GET requests gracefully HOT 1
- Getting 500 response after login redirect HOT 4
- I can't create user and organization by `createKindeManagementAPIClient`. HOT 5
- Bug: getUser function not returning a username property HOT 3
- Bug: getRoles return null even I've set role for user HOT 3
- Feature request: Get users phone number from getUser() function HOT 1
- Feature request: Allow passing given_name and family_name in authUrlParams for custom signup process HOT 1
- Bug: isReturnToCurrentPage middleware setting HOT 3
- Documentation issue: getKindeBrowserClient does not exist HOT 1
- Bug: getUser() return null on live (NextJs) HOT 3
- Bug: api/auth/setup firing 90 times on page load HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kinde-auth-nextjs.