Fix Codex auth import and reset credit details
# Conflicts: # app/(tabs)/codex.tsx # app/(tabs)/index.tsx # app/(tabs)/settings.tsx # hooks/useCodexUsage.ts # lib/api/codexApi.ts # package-lock.json # package.json
This commit is contained in:
+42
-4
@@ -1,6 +1,41 @@
|
||||
import * as DocumentPicker from "expo-document-picker";
|
||||
import * as LegacyFileSystem from "expo-file-system/legacy";
|
||||
import type { CodexAuth } from "@/types/codex";
|
||||
|
||||
function sanitizeFileName(name: string): string {
|
||||
return name.replace(/[^a-zA-Z0-9._-]/g, "_");
|
||||
}
|
||||
|
||||
async function readPickedTextFile(uri: string, fileName?: string): Promise<string> {
|
||||
try {
|
||||
const response = await fetch(uri);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP_ERROR_${response.status}`);
|
||||
}
|
||||
return await response.text();
|
||||
} catch {
|
||||
try {
|
||||
return await LegacyFileSystem.readAsStringAsync(uri);
|
||||
} catch {
|
||||
const cacheDirectory = LegacyFileSystem.cacheDirectory;
|
||||
if (!cacheDirectory) {
|
||||
throw new Error("DOCUMENT_NOT_READABLE");
|
||||
}
|
||||
|
||||
const destination = `${cacheDirectory}${Date.now()}-${sanitizeFileName(
|
||||
fileName ?? "import.json"
|
||||
)}`;
|
||||
|
||||
try {
|
||||
await LegacyFileSystem.copyAsync({ from: uri, to: destination });
|
||||
return await LegacyFileSystem.readAsStringAsync(destination);
|
||||
} catch {
|
||||
throw new Error("DOCUMENT_NOT_READABLE");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function pickAndReadCodexAuth(): Promise<CodexAuth> {
|
||||
const result = await DocumentPicker.getDocumentAsync({
|
||||
type: "application/json",
|
||||
@@ -9,9 +44,8 @@ export async function pickAndReadCodexAuth(): Promise<CodexAuth> {
|
||||
|
||||
if (result.canceled) throw new Error("PICKER_CANCELLED");
|
||||
|
||||
const { uri } = result.assets[0];
|
||||
const response = await fetch(uri);
|
||||
const text = await response.text();
|
||||
const { uri, name } = result.assets[0];
|
||||
const text = await readPickedTextFile(uri, name);
|
||||
|
||||
let parsed: Record<string, unknown>;
|
||||
try {
|
||||
@@ -22,7 +56,11 @@ export async function pickAndReadCodexAuth(): Promise<CodexAuth> {
|
||||
|
||||
const tokens = parsed["tokens"] as Record<string, unknown> | undefined;
|
||||
const accessToken = (tokens?.["access_token"] ?? parsed["accessToken"]) as string | undefined;
|
||||
const accountId = (tokens?.["account_id"] ?? parsed["accountId"]) as string | undefined;
|
||||
const accountId = (
|
||||
tokens?.["account_id"] ??
|
||||
parsed["accountId"] ??
|
||||
parsed["account_id"]
|
||||
) as string | undefined;
|
||||
|
||||
if (!accessToken || typeof accessToken !== "string") {
|
||||
throw new Error("MISSING_ACCESS_TOKEN");
|
||||
|
||||
Reference in New Issue
Block a user