asyarb / react-use-googlelogin

A React hook wrapper around Google's client JavaScript oAuth2 library.
MIT License
58 stars 11 forks source link

refreshUser function doesn't update googleUser #36

Open pszafer opened 3 years ago

pszafer commented 3 years ago

Hi,

I basically copied code from this example: https://github.com/asyarb/react-use-googlelogin/blob/34f5b437a36d694859e113a4305b37a76f86bd91/examples/ssr-nextjs/src/components/GoogleAuthProvider.tsx#L93

  const fetchWithRefresh: ContextValue['fetchWithRefresh'] = async (
    input,
    init
  ) => {
    let tokenId = googleUser.tokenId
    const shouldRefreshToken = googleUser.expiresAt - 300 * 1000 - Date.now() <= 0

    if (shouldRefreshToken) {
      const tokenObj = await refreshUser()
      tokenId = googleUser.tokenId
    }

    return fetch(input, {
      ...init,
      headers: {
        ...init?.headers,
        Authorization: `Bearer ${tokenId}`,
      },
    })
  }

refreshUser() never refreshses expiresAt value so after an hour when first achieved token expired with each fetch app is going into this if if (shouldRefreshToken)

pszafer commented 3 years ago

it seems that for some reason setState is not working (it is called but old value of googleUser is available in fetchWithRefresh)... https://github.com/asyarb/react-use-googlelogin/blob/093862e99c3a9b9372a33516ebe6e59cc682785d/src/index.tsx#L222-L242