Hey there,
I’ve been working on an assignment from the React course I’m taking, one of the tasks asks you to logout the user when he gets a 401 status code and reload the page.
function client(
endpoint,
{data, token, headers: customHeaders, ...customConfig} = {},
) {
const config = {
method: data ? 'POST' : 'GET',
body: data ? JSON.stringify(data) : undefined,
headers: {
Authorization: token ? `Bearer ${token}` : undefined,
'Content-Type': data ? 'application/json' : undefined,
...customHeaders,
},
...customConfig,
}
return window.fetch(`${apiURL}/${endpoint}`, config).then(async response => {
if (response.status === 401) {
await auth.logout()
// refresh the page for them
window.location.assign(window.location)
return Promise.reject({message: 'Please re-authenticate.'})
}
const data = await response.json()
if (response.ok) {
return data
} else {
return Promise.reject(data)
}
})
}
My question is about this part from their solution :
if (response.status === 401) {
await auth.logout() // deletes token from local storage
// refresh the page for them
window.location.assign(window.location)
return Promise.reject({message: 'Please re-authenticate.'})
}
if window.location.assign(window.location)
is executed, won’t that refresh the page, meaning that the function will never get to that last return statement?
or am I missing something?
Any help would be appreciated