LocChat/client/utils/use_jwt_refresh.js
2021-11-23 14:04:12 -07:00

20 lines
596 B
JavaScript

import { useEffect, useRef } from 'react';
export const useJwtRefresh = (authToken, setAuthToken) => {
const refreshTimer = useRef(null);
useEffect(() => {
clearTimeout(refreshTimer.current);
if (authToken) {
refreshTimer.current = setTimeout(async () => {
const result = await fetch('/refresh_token').then((res) => res.json());
if (result.token) {
setAuthToken(result.token);
} else {
setAuthToken(null);
}
}, 60000 * 10); // 10 minutes
}
return () => clearTimeout(refreshTimer.current);
}, [authToken]);
};