Files
codexbar-mobile/lib/fileReader.ts
T

35 lines
919 B
TypeScript

import * as DocumentPicker from "expo-document-picker";
import { File } from "expo-file-system/next";
import type { CodexAuth } from "@/types/codex";
export async function pickAndReadCodexAuth(): Promise<CodexAuth> {
const result = await DocumentPicker.getDocumentAsync({
type: "application/json",
copyToCacheDirectory: true,
});
if (result.canceled) {
throw new Error("PICKER_CANCELLED");
}
const { uri } = result.assets[0];
const file = new File(uri);
const text = file.text();
let parsed: Record<string, unknown>;
try {
parsed = JSON.parse(text);
} catch {
throw new Error("INVALID_JSON");
}
const accessToken = parsed["accessToken"] as string | undefined;
const accountId = parsed["accountId"] as string | undefined;
if (!accessToken || typeof accessToken !== "string") {
throw new Error("MISSING_ACCESS_TOKEN");
}
return { accessToken, accountId };
}