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 11, 2023
1 parent 72092e5 commit 472cf3d
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 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,14 +318,14 @@ 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.')
return
}

Api.postToken(
return Api.postToken(
{ token: session.auth.token, signal: abortCtrl.signal },
{
grant_type: 'refresh_token',
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 472cf3d

Please sign in to comment.