Skip to content

Commit

Permalink
Handle auth errors
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesPeck committed Oct 18, 2024
1 parent e001085 commit 3a374e5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
24 changes: 10 additions & 14 deletions src/lib/services/dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,6 @@ export function searchDictionary(
);
}

export async function getAllFacets(): Promise<DictionaryFacetResult[]> {
let request: DictionarySearchRequest = { facets: [], search: '' };
if (!get(page).url.pathname.includes('/discover')) {
request = addConsents(request);
}
const response: DictionaryFacetResult[] = await api.post(`${dictionaryUrl}facets/`, request);
initializeHiddenFacets(response);
return response;
}

function initializeHiddenFacets(response: DictionaryFacetResult[]) {
// facets that have a count of zero should never be shown in the UI
// this happens because of consent filters
Expand Down Expand Up @@ -87,11 +77,17 @@ export async function updateFacetsFromSearch(
request = addConsents(request);
}

const response: DictionaryFacetResult[] = await api.post(`${dictionaryUrl}facets/`, request);
if (facets.length === 0 && search.length === 0) {
initializeHiddenFacets(response);
try {
const response: DictionaryFacetResult[] = await api.post(`${dictionaryUrl}facets/`, request);
if (facets.length === 0 && search.length === 0) {
initializeHiddenFacets(response);
}
return response;
} catch (error) {
console.error('Failed to update facets from search:', error);
// You might want to throw the error or return a default value depending on your use case
return [];
}
return response;
}

export async function getConceptDetails(
Expand Down
8 changes: 6 additions & 2 deletions src/lib/stores/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,12 @@ export async function login(token: string) {
export async function logout(authProvider?: AuthProvider, redirect = false) {
if (browser) {
const token = localStorage.getItem('token');
token && api.get('/psama/logout');
token && localStorage.removeItem('token');
if (token) {
api.get('/psama/logout').catch((error) => {
console.error('Error logging out: ' + error);
});
localStorage.removeItem('token');
}
}

// get the auth provider
Expand Down

0 comments on commit 3a374e5

Please sign in to comment.