Enhance Discord authentication and guild management by implementing caching for user and guild data to reduce API calls and improve performance. Update error handling for Discord API responses and refactor the guilds listing logic in the dashboard to streamline data retrieval. Adjust retry logic for rate limits and improve session error handling in the authentication flow.
This commit is contained in:
@@ -79,8 +79,14 @@ export const authOptions: AuthOptions = {
|
||||
}
|
||||
|
||||
const expiresAt = typeof token.expiresAt === "number" ? token.expiresAt : 0;
|
||||
// Refresh ~60s before expiry
|
||||
if (Date.now() < expiresAt * 1000 - 60_000) {
|
||||
// Refresh when expired, missing expiry, or previous refresh failed
|
||||
const stillValid =
|
||||
Boolean(token.accessToken) &&
|
||||
expiresAt > 0 &&
|
||||
Date.now() < expiresAt * 1000 - 60_000 &&
|
||||
!token.error;
|
||||
|
||||
if (stillValid) {
|
||||
return token;
|
||||
}
|
||||
|
||||
@@ -100,6 +106,11 @@ export const authOptions: AuthOptions = {
|
||||
// @ts-ignore
|
||||
session.error = token.error;
|
||||
}
|
||||
// Surface refresh failures so pages can redirect to sign-in
|
||||
if (token.error) {
|
||||
// @ts-ignore
|
||||
session.error = token.error;
|
||||
}
|
||||
return session;
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user