Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle fetch top-level errors #618

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 47 additions & 41 deletions src/useFirebaseUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,32 @@ const defaultTokenChangedHandler = async (authUser) => {
// place we use this logic.
logDebug('[withAuthUser] Calling the login endpoint.')
const userToken = await authUser.getIdToken()
response = await fetch(loginAPIEndpoint, {
method: 'POST',
headers: {
Authorization: userToken,
},
credentials: 'include',
})
if (!response.ok) {
const responseJSON = await response.json()
logDebug(
`[withAuthUser] The call to the login endpoint failed with status ${
response.status
} and response: ${JSON.stringify(responseJSON)}`
)
try {
response = await fetch(loginAPIEndpoint, {
method: 'POST',
headers: {
Authorization: userToken,
},
credentials: 'include',
})
if (!response.ok) {
const responseJSON = await response.json()
logDebug(
`[withAuthUser] The call to the login endpoint failed with status ${
response.status
} and response: ${JSON.stringify(responseJSON)}`
)

// If the developer provided a handler for login errors,
// call it and don't throw.
// https://github.com/gladly-team/next-firebase-auth/issues/367
const err = new Error(
`Received ${
response.status
} response from login API endpoint: ${JSON.stringify(responseJSON)}`
)
// If the developer provided a handler for login errors,
// call it and don't throw.
// https://github.com/gladly-team/next-firebase-auth/issues/367
throw new Error(
`Received ${
response.status
} response from login API endpoint: ${JSON.stringify(responseJSON)}`
)
}
} catch (err) {
if (onLoginRequestError) {
await onLoginRequestError(err)
} else {
Expand All @@ -52,26 +55,29 @@ const defaultTokenChangedHandler = async (authUser) => {
} else {
// If the user is not authed, call logout to unset the cookie.
logDebug('[withAuthUser] Calling the logout endpoint.')
response = await fetch(logoutAPIEndpoint, {
method: 'POST',
credentials: 'include',
})
if (!response.ok) {
const responseJSON = await response.json()
logDebug(
`[withAuthUser] The call to the logout endpoint failed with status ${
response.status
} and response: ${JSON.stringify(responseJSON)}`
)
try {
response = await fetch(logoutAPIEndpoint, {
method: 'POST',
credentials: 'include',
})
if (!response.ok) {
const responseJSON = await response.json()
logDebug(
`[withAuthUser] The call to the logout endpoint failed with status ${
response.status
} and response: ${JSON.stringify(responseJSON)}`
)

// If the developer provided a handler for logout errors,
// call it and don't throw.
// https://github.com/gladly-team/next-firebase-auth/issues/367
const err = new Error(
`Received ${
response.status
} response from logout API endpoint: ${JSON.stringify(responseJSON)}`
)
// If the developer provided a handler for logout errors,
// call it and don't throw.
// https://github.com/gladly-team/next-firebase-auth/issues/367
throw new Error(
`Received ${
response.status
} response from logout API endpoint: ${JSON.stringify(responseJSON)}`
)
}
} catch (err) {
if (onLogoutRequestError) {
await onLogoutRequestError(err)
} else {
Expand Down