working auth flow

This commit is contained in:
Violet Caulfield
2024-10-15 13:10:04 -05:00
parent 336e1cd6d9
commit 517c5dbab7
10 changed files with 47 additions and 14 deletions

View File

@@ -2,12 +2,12 @@ import { useMutation } from "@tanstack/react-query";
import { JellyfinCredentials } from "../types/jellyfin-credentials";
import { MutationKeys } from "../../enums/mutation-keys";
import { createPublicApi } from "../queries/functions/api";
import { fetchServerUrl } from "../queries/functions/storage";
import { fetchServer } from "../queries/functions/storage";
export const authenticateWithCredentials = useMutation({
mutationKey: [MutationKeys.AuthenticationWithCredentials],
mutationFn: async (credentials: JellyfinCredentials) => {
createPublicApi(await fetchServerUrl)
createPublicApi((await fetchServer()).url)
.authenticateUserByName(credentials.username, credentials.password!);
},
onSuccess(data, credentials, context) {

View File

@@ -1,9 +1,9 @@
import { fetchServerUrl } from "../../queries/functions/storage";
import { fetchServer } from "../../queries/functions/storage";
import { JellyfinCredentials } from "../../types/jellyfin-credentials";
import * as Keychain from "react-native-keychain"
export const mutateServerCredentials = async (credentials: JellyfinCredentials) => {
return Keychain.setInternetCredentials(await fetchServerUrl, credentials.username, credentials.accessToken!);
return Keychain.setInternetCredentials((await fetchServer()).url, credentials.username, credentials.accessToken!);
}

View File

@@ -26,6 +26,13 @@ export const serverUrlMutation = useMutation({
throw new Error("Unable to connect to Jellyfin Server");
console.log(`Connected to Jellyfin ${publicSystemInfoResponse.data.Version!}`);
// TODO: Store these along side address
// TODO: Rename url to address
publicSystemInfoResponse.data.ServerName;
publicSystemInfoResponse.data.StartupWizardCompleted;
publicSystemInfoResponse.data.Version;
return AsyncStorage.setItem(AsyncStorageKeys.ServerUrl, serverUrl!);
}
});

View File

@@ -4,7 +4,7 @@ import { getDeviceNameSync, getUniqueIdSync } from "react-native-device-info"
import { QueryKeys } from "../enums/query-keys";
import { name, version } from "../package.json"
import { createApi, createPublicApi } from "./queries/functions/api";
import { fetchServerUrl } from "./queries/functions/storage";
import { fetchServer } from "./queries/functions/storage";
export const client : Jellyfin = new Jellyfin({
clientInfo: {

View File

@@ -4,7 +4,7 @@ import { QueryKeys } from "../../enums/query-keys";
import { useApi } from "../queries";
import { BaseItemDto } from "@jellyfin/sdk/lib/generated-client/models";
import { useChildrenFromParent } from "./items";
import { fetchServerUrl } from "./functions/storage";
import { fetchServer } from "./functions/storage";
import { createApi } from "./functions/api";
export const useArtistAlbums : (artistId: string) => UseQueryResult<BaseItemDto[], Error> = (artistId: string) => useQuery({

View File

@@ -2,6 +2,7 @@ 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"
import { JellifyServer } from "../../../types/JellifyServer";
export const fetchCredentials : () => Promise<Keychain.SharedWebCredentials> = () => new Promise(async (resolve, reject) => {
@@ -23,14 +24,14 @@ export const fetchCredentials : () => Promise<Keychain.SharedWebCredentials> = (
resolve(keychain as Keychain.SharedWebCredentials)
});
export const fetchServerUrl : () => Promise<string> = () => new Promise(async (resolve, reject) => {
export const fetchServer : () => Promise<JellifyServer> = () => new Promise(async (resolve, reject) => {
console.log("Attempting to fetch server address from storage");
let url = await AsyncStorage.getItem(AsyncStorageKeys.ServerUrl);
let serverJson = await AsyncStorage.getItem(AsyncStorageKeys.ServerUrl);
if (_.isNull(url))
if (_.isNull(serverJson))
throw new Error("No stored server address exists");
resolve(url);
resolve(JSON.parse(serverJson) as JellifyServer);
});

View File

@@ -1,7 +1,7 @@
import { useQuery } from "@tanstack/react-query"
import { QueryKeys } from "../../enums/query-keys"
import _ from "lodash";
import { fetchCredentials, fetchServerUrl } from "./functions/storage";
import { fetchCredentials, fetchServer } from "./functions/storage";
export const useCredentials = useQuery({
queryKey: [QueryKeys.Credentials],
@@ -10,5 +10,5 @@ export const useCredentials = useQuery({
export const useServerUrl = useQuery({
queryKey: [QueryKeys.ServerUrl],
queryFn: fetchServerUrl
queryFn: fetchServer
})

View File

@@ -9,6 +9,7 @@ import { client } from "../../../api/queries";
import { AsyncStorageKeys } from "../../../enums/async-storage-keys";
import { ActivityIndicator } from "react-native";
import { Button, TextField, View } from "react-native-ui-lib";
import { JellifyServer } from "../../../types/JellifyServer";
export default function ServerAddress(): React.JSX.Element {
@@ -34,10 +35,19 @@ export default function ServerAddress(): React.JSX.Element {
if (!!!publicSystemInfoResponse.data.Version)
throw new Error("Jellyfin instance did not respond");
console.debug("REMOVE THIS::Public System Info Response", publicSystemInfoResponse);
console.debug("REMOVE THIS::onSuccess variable", serverUrl);
console.log(`Connected to Jellyfin ${publicSystemInfoResponse.data.Version!}`);
return AsyncStorage.setItem(AsyncStorageKeys.ServerUrl, serverUrl);
// TODO: Store these along side address
// TODO: Rename url to address
let jellifyServer: JellifyServer = {
url: serverUrl,
name: publicSystemInfoResponse.data.ServerName!,
version: publicSystemInfoResponse.data.Version!,
startUpComplete: publicSystemInfoResponse.data.StartupWizardCompleted!
}
return AsyncStorage.setItem(AsyncStorageKeys.ServerUrl, JSON.stringify(jellifyServer));
},
onError: (error: Error) => {
console.error("An error occurred connecting to the Jellyfin instance", error);

6
types/JellifyLibrary.ts Normal file
View File

@@ -0,0 +1,6 @@
export interface JellifyLibrary {
musicLibraryId: string;
musicLibraryName: string;
playlistLibraryId: string;
}

9
types/JellifyServer.ts Normal file
View File

@@ -0,0 +1,9 @@
import { JellifyLibrary } from "./JellifyLibrary";
export interface JellifyServer {
url: string;
name: string;
version: string;
startUpComplete: boolean;
library?: JellifyLibrary;
}