react-use-googlelogin
react-use-googlelogin copied to clipboard
googleUser is undefined after logout
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.