Phase 6 complete
This commit is contained in:
parent
93ea01525e
commit
3a22b42492
11 changed files with 735 additions and 103 deletions
|
|
@ -36,17 +36,29 @@ export const useAuthenticatedFetch = () => {
|
|||
|
||||
const authFetch = useCallback(
|
||||
async (path: string, options: RequestInit = {}): Promise<Response> => {
|
||||
if (!navigator.onLine) {
|
||||
throw new Error('You appear to be offline. Please check your connection.');
|
||||
}
|
||||
|
||||
const token = await ensureValidToken();
|
||||
const url = path.startsWith('http') ? path : `${API_URL}${path}`;
|
||||
|
||||
const response = await fetch(url, {
|
||||
...options,
|
||||
headers: {
|
||||
...options.headers,
|
||||
Authorization: `Bearer ${token}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
let response: Response;
|
||||
try {
|
||||
response = await fetch(url, {
|
||||
...options,
|
||||
headers: {
|
||||
...options.headers,
|
||||
Authorization: `Bearer ${token}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
if (!navigator.onLine) {
|
||||
throw new Error('You appear to be offline. Please check your connection.');
|
||||
}
|
||||
throw new Error('Network error. Please try again.');
|
||||
}
|
||||
|
||||
if (response.status === 401) {
|
||||
try {
|
||||
|
|
@ -66,6 +78,10 @@ export const useAuthenticatedFetch = () => {
|
|||
}
|
||||
throw new Error('Session expired, redirecting to login');
|
||||
}
|
||||
|
||||
if (response.status >= 500) {
|
||||
throw new Error('Server error. Please try again later.');
|
||||
}
|
||||
|
||||
return response;
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue