The minimalistic document archiving platform.
fix(mobile): stop redirecting to org creation when invalid server (#833)
Corentin Thomassetf7b03368d6ed
diff --git a/apps/mobile/src/modules/api/http.client.ts b/apps/mobile/src/modules/api/http.client.tsindex b1c5b60e..9459c9c9 100644--- a/apps/mobile/src/modules/api/http.client.ts+++ b/apps/mobile/src/modules/api/http.client.ts@@ -1,5 +1,5 @@-import type { FetchOptions, ResponseType } from 'ofetch';-import { ofetch } from 'ofetch';+import type { FetchOptions } from 'ofetch';+import { FetchError, ofetch, ResponseType } from 'ofetch';export { ResponseType };export type HttpClientOptions<R extends ResponseType = 'json'> = Omit<FetchOptions<R>, 'baseURL'> & { url: string; baseUrl?: string };@@ -10,3 +10,7 @@ export async function httpClient<A, R extends ResponseType = 'json'>({ url, base...rest,});}++export function isHttpClientError(error: unknown): error is FetchError {+ return error instanceof FetchError;+}diff --git a/apps/mobile/src/modules/organizations/organizations.provider.tsx b/apps/mobile/src/modules/organizations/organizations.provider.tsxindex c07ac122..92c2fab0 100644--- a/apps/mobile/src/modules/organizations/organizations.provider.tsx+++ b/apps/mobile/src/modules/organizations/organizations.provider.tsx@@ -3,6 +3,7 @@ import type { Organization } from '@/modules/organizations/organizations.types';import { useQuery, useQueryClient } from '@tanstack/react-query';import { useRouter } from 'expo-router';import { createContext, useContext, useEffect, useState } from 'react';+import { isHttpClientError } from '../api/http.client';import { useApiClient } from '../api/providers/api.provider';import { organizationsLocalStorage } from './organizations.local-storage';import { fetchOrganizations } from './organizations.services';@@ -49,12 +50,23 @@ export function OrganizationsProvider({ children }: OrganizationsProviderProps)setCurrentOrganizationIdState(organizationId);};- // Redirect to organization selection if no organizations or no current org setuseEffect(() => {if (!isInitialized || organizationsQuery.isLoading) {return;}+ if (organizationsQuery.error) {+ const error = organizationsQuery.error;++ if (isHttpClientError(error) && error.status === 401) {+ router.replace('/auth/login');+ return;+ }++ router.replace('/config/server-selection');+ return;+ }+const organizations = organizationsQuery.data?.organizations ?? [];if (organizations.length === 0) {