| @@ -48,7 +48,18 @@ const RefreshTokenProvider = ({ children }) => { | |||||
| const checkTokenExpiry = useCallback(() => { | const checkTokenExpiry = useCallback(() => { | ||||
| // Check if token is present and its expiry time | // Check if token is present and its expiry time | ||||
| if (token.current) { | if (token.current) { | ||||
| const tokenExp = JSON.parse(atob(token.current.split('.')[1])).exp; | |||||
| let tokenExp; | |||||
| try { | |||||
| // JWT payload is base64url; atob needs standard base64 | |||||
| let payload = token.current.split('.')[1] | |||||
| .replace(/-/g, '+') | |||||
| .replace(/_/g, '/'); | |||||
| payload += '='.repeat((4 - (payload.length % 4)) % 4); | |||||
| tokenExp = JSON.parse(atob(payload)).exp; | |||||
| } catch (e) { | |||||
| token.current = null; | |||||
| return; | |||||
| } | |||||
| const currentTime = Math.floor(Date.now() / 1000); | const currentTime = Math.floor(Date.now() / 1000); | ||||
| const expiryTime = tokenExp - 30; // Refresh 30 seconds before expiry | const expiryTime = tokenExp - 30; // Refresh 30 seconds before expiry | ||||
| // console.log("check refresh Token"); | // console.log("check refresh Token"); | ||||