react-use-googlelogin icon indicating copy to clipboard operation
react-use-googlelogin copied to clipboard

googleUser is undefined after logout

Open pszafer opened this issue 4 years ago • 0 comments

Hello again, Just to remind, I'm using Gatsby SSR.

After user click logout there is an error in function fetchWithRefresh

const fetchWithRefresh = async (input, init) => {
    let accessToken = googleUser.accessToken
    // The token is within 5 minutes of expiring
    const shouldRefreshToken =
      googleUser.expiresAt - 300 * 1000 - Date.now() <= 0
    if (shouldRefreshToken) {
      const tokenObj = await refreshUser()
      accessToken = tokenObj?.accessToken ?? accessToken
    }

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

to fix I just return sooner if googleUser is undefined:

const fetchWithRefresh = async (input, init) => {
    if (!googleUser) return;
    let accessToken = googleUser.accessToken
    // The token is within 5 minutes of expiring
    const shouldRefreshToken =
      googleUser.expiresAt - 300 * 1000 - Date.now() <= 0
    if (shouldRefreshToken) {
      const tokenObj = await refreshUser()
      accessToken = tokenObj?.accessToken ?? accessToken
    }

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

Not sure if it is a bug, but if not then I think it should be shown in example docs.

pszafer avatar Sep 21 '20 10:09 pszafer