RefreshToken.jsx 690 Bytes
import React from 'react'

import { ReduxAstroProvider } from 'astro-redux/react'
import { useGetTokenQuery } from 'astro-redux/slices'

const RefreshToken = ReduxAstroProvider((props) => {
  const [ retry, setRetry ] = React.useState(props.retry)
  React.useEffect(() => {
    if (retry !== props.retry) setRetry(props.retry)
  }, [ props.retry ])

  const getTokenQueryResult = useGetTokenQuery(undefined, { pollingInterval: retry })
  const { data: { expires_at, access_token } = {} } = getTokenQueryResult
  React.useEffect(() => {
    if (!access_token) setRetry(0)
  }, [ access_token ])

  return null
})
RefreshToken.defaultProps = {
  retry: 60000,
}

export default RefreshToken