lukas
lukas
BABetter Auth
Created by lukas on 4/5/2025 in #help
Session cookie not refreshing
Seems to be fixed in 1.2.6-beta.5
7 replies
BABetter Auth
Created by lukas on 4/5/2025 in #help
Session cookie not refreshing
1.2.5
7 replies
BABetter Auth
Created by lukas on 3/24/2025 in #help
Dealing with session expiration in a React app
const originalUseSession = authClient.useSession;

// Create our extended useSession hook with expiration handling
export function useSession() {
const originalResult = originalUseSession();
const [sessionResult, setSessionResult] = useState(originalResult);

useEffect(() => {
// Update sessionResult whenever originalResult changes
setSessionResult(originalResult);
}, [originalResult]);

useEffect(() => {
const { data } = sessionResult;

// If no session or no expiration, don't set up a timer
if (!data?.session) return;
if (!data.session?.expiresAt) return;

// Calculate time until expiration
const expiresAt = new Date(data.session.expiresAt).getTime();
const refetchIn = expiresAt - Date.now() + 5000; // fetch the session 5 seconds after it expires

const timeout = setTimeout(async () => {
const newSessionResult = await authClient.getSession();

if (!newSessionResult || !newSessionResult.data || newSessionResult.data === null) {
setSessionResult({ ...sessionResult, data: null });
}
}, refetchIn);

return () => clearTimeout(timeout);
}, [sessionResult]);

return sessionResult;
}
const originalUseSession = authClient.useSession;

// Create our extended useSession hook with expiration handling
export function useSession() {
const originalResult = originalUseSession();
const [sessionResult, setSessionResult] = useState(originalResult);

useEffect(() => {
// Update sessionResult whenever originalResult changes
setSessionResult(originalResult);
}, [originalResult]);

useEffect(() => {
const { data } = sessionResult;

// If no session or no expiration, don't set up a timer
if (!data?.session) return;
if (!data.session?.expiresAt) return;

// Calculate time until expiration
const expiresAt = new Date(data.session.expiresAt).getTime();
const refetchIn = expiresAt - Date.now() + 5000; // fetch the session 5 seconds after it expires

const timeout = setTimeout(async () => {
const newSessionResult = await authClient.getSession();

if (!newSessionResult || !newSessionResult.data || newSessionResult.data === null) {
setSessionResult({ ...sessionResult, data: null });
}
}, refetchIn);

return () => clearTimeout(timeout);
}, [sessionResult]);

return sessionResult;
}
13 replies
BABetter Auth
Created by lukas on 3/24/2025 in #help
Dealing with session expiration in a React app
So I have a custom useSession hook now - I tried calling refetch() after expiration to get the native useSession to refresh with the null session, but it didn't work, I had to just return data: null from my custom useSession hook after the getSession call from the timeout returns null. Does that seem right?
13 replies
BABetter Auth
Created by lukas on 3/24/2025 in #help
Dealing with session expiration in a React app
Basically - I need the UI to change as soon as the session expires. Seems like my options are to poll with getSession, or to just wait until an operation fails with a 401 error.
13 replies
BABetter Auth
Created by lukas on 3/24/2025 in #help
Dealing with session expiration in a React app
Yeah, an auth guard on the frontend is what I'm trying to set up. The problem is that useSession does not fire if the session expires (confirmed by bekacru, see my discussion link above.)
13 replies