Skip to content

Commit

Permalink
chore(session): reschedule renew token request manually
Browse files Browse the repository at this point in the history
  • Loading branch information
theborakompanioni committed Oct 10, 2023
1 parent 72092e5 commit c53245c
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/context/WalletContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as Api from '../libs/JmWalletApi'
import { WalletBalanceSummary, toBalanceSummary } from './BalanceSummary'
import { JM_API_AUTH_TOKEN_EXPIRY } from '../constants/config'
import { isDevMode } from '../constants/debugFeatures'
import { walletDisplayName } from '../utils'
import { setIntervalDebounced, walletDisplayName } from '../utils'

const API_AUTH_TOKEN_RENEW_INTERVAL: Milliseconds = isDevMode()
? 60 * 1_000
Expand Down Expand Up @@ -318,7 +318,7 @@ const WalletProvider = ({ children }: PropsWithChildren<any>) => {

const abortCtrl = new AbortController()

const renewToken = () => {
const renewToken = async () => {
const session = getSession()
if (!session?.auth?.refresh_token) {
console.warn('Cannot renew auth token - no refresh_token available.')
Expand All @@ -341,12 +341,15 @@ const WalletProvider = ({ children }: PropsWithChildren<any>) => {
console.debug('Successfully renewed auth token.')
})
.catch((err) => {
if (abortCtrl.signal.aborted) return
console.error(err)
if (!abortCtrl.signal.aborted) {
console.error(err)
}
})
}

const interval = setInterval(renewToken, API_AUTH_TOKEN_RENEW_INTERVAL)
let interval: NodeJS.Timer
setIntervalDebounced(renewToken, API_AUTH_TOKEN_RENEW_INTERVAL, (timerId) => (interval = timerId))

return () => {
clearInterval(interval)
abortCtrl.abort()
Expand Down

0 comments on commit c53245c

Please sign in to comment.