diff --git a/api/queries.ts b/api/queries.ts index 9f6b504d..3cead1d0 100644 --- a/api/queries.ts +++ b/api/queries.ts @@ -24,10 +24,10 @@ export const usePublicApi = (serverUrl: string) => useQuery({ } }) -export const useApi = (serverUrl: string) => useQuery({ - queryKey: [QueryKeys.Api, serverUrl], +export const useApi = () => useQuery({ + queryKey: [QueryKeys.Api], queryFn: async ({ queryKey }) => { - createApi(queryKey[1]); + createApi(); } }) diff --git a/api/queries/albums.ts b/api/queries/albums.ts index b1720a80..5846a089 100644 --- a/api/queries/albums.ts +++ b/api/queries/albums.ts @@ -10,7 +10,7 @@ import { createApi } from "./functions/api"; export const useArtistAlbums : (artistId: string) => UseQueryResult = (artistId: string) => useQuery({ queryKey: [QueryKeys.ArtistAlbums, artistId], queryFn: async ({ queryKey }) => { - return getItemsApi(await createApi(await fetchServerUrl)) + return getItemsApi(await createApi()) .getItems({ albumArtistIds: [queryKey[1]] }) .then((result) => { return result.data.Items diff --git a/api/queries/functions/api.ts b/api/queries/functions/api.ts index 369176f5..bcbb7c5c 100644 --- a/api/queries/functions/api.ts +++ b/api/queries/functions/api.ts @@ -1,10 +1,9 @@ import { Api } from "@jellyfin/sdk"; import { client } from "../../queries"; -import { fetchCredentials } from "./keychain"; +import { fetchCredentials } from "./storage"; - -export const createApi: (serverUrl: string) => Promise = async (serverUrl) => { - let credentials = await fetchCredentials(serverUrl) +export const createApi: () => Promise = async () => { + let credentials = await fetchCredentials() return client.createApi(credentials.server, credentials.password); } diff --git a/api/queries/functions/storage.ts b/api/queries/functions/storage.ts index 7e8cd663..f04ab013 100644 --- a/api/queries/functions/storage.ts +++ b/api/queries/functions/storage.ts @@ -1,8 +1,29 @@ import AsyncStorage from "@react-native-async-storage/async-storage" import { AsyncStorageKeys } from "../../../enums/async-storage-keys" import _ from "lodash"; +import * as Keychain from "react-native-keychain" +export const fetchCredentials : () => Promise = () => new Promise(async (resolve, reject) => { + + console.log("Attempting to use stored credentials"); + + let serverUrl = await AsyncStorage.getItem(AsyncStorageKeys.ServerUrl); + + console.debug(`REMOVE THIS::Server Url ${serverUrl}`); + + if (!_.isNull(serverUrl)) + Keychain.getInternetCredentials(serverUrl!) + .then((keychain) => { + if (!keychain) + reject(new Error("Unable to retrieve credentials for server address")) + + resolve(keychain as Keychain.SharedWebCredentials) + }) + + reject(new Error("Unable to retrieve credentials without a server URL")); +}); + export const fetchServerUrl : Promise = new Promise(async (resolve, reject) => { console.log("Attempting to fetch server address from storage"); diff --git a/api/queries/keychain.ts b/api/queries/keychain.ts index 32cbecc2..3dd16d14 100644 --- a/api/queries/keychain.ts +++ b/api/queries/keychain.ts @@ -1,24 +1,9 @@ import { useQuery } from "@tanstack/react-query" import { QueryKeys } from "../../enums/query-keys" -import { fetchServerUrl } from "./functions/storage"; import _ from "lodash"; -import * as Keychain from "react-native-keychain" -import AsyncStorage from "@react-native-async-storage/async-storage"; -import { AsyncStorageKeys } from "../../enums/async-storage-keys"; +import { fetchCredentials } from "./functions/storage"; export const useCredentials = useQuery({ queryKey: [QueryKeys.Credentials], - queryFn: async () => { - - console.log("Attempting to use stored credentials"); - - let serverUrl = await AsyncStorage.getItem(AsyncStorageKeys.ServerUrl); - - console.debug(`REMOVE THIS::Server Url ${serverUrl}`); - - if (!_.isNull(serverUrl)) - return Keychain.getInternetCredentials(serverUrl!); - - return new Error("Unable to retrieve credentials without a server URL") - } + queryFn: fetchCredentials }); \ No newline at end of file diff --git a/api/queries/playlist.ts b/api/queries/playlist.ts index d569a482..958bc0af 100644 --- a/api/queries/playlist.ts +++ b/api/queries/playlist.ts @@ -2,12 +2,11 @@ import { useQuery } from "@tanstack/react-query"; import { QueryKeys } from "../../enums/query-keys"; import { getPlaylistsApi } from "@jellyfin/sdk/lib/utils/api/playlists-api" import { createApi } from "./functions/api"; -import { fetchServerUrl } from "./functions/storage"; export const usePlaylists = useQuery({ queryKey: [QueryKeys.Playlists], queryFn: async () => { - return getPlaylistsApi(await createApi(await fetchServerUrl)) + return getPlaylistsApi(await createApi()) } }) \ No newline at end of file