问题: - NativeStorage 使用了同步方法调用 expo-secure-store - expo-secure-store 的正确 API 是异步的(getItemAsync/setItemAsync/deleteItemAsync) - 在 Android 上同步调用不存在的方法导致应用崩溃 修复: - 将 Storage 接口改为异步方法 - 使用 SecureStore.getItemAsync/setItemAsync/deleteItemAsync - removeItem 使用 deleteItemAsync 而不是设置空字符串 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
import * as SecureStore from "expo-secure-store";
|
|
import { Platform } from "react-native";
|
|
|
|
interface Storage {
|
|
getItem(key: string): Promise<string | null>;
|
|
setItem(key: string, value: string): Promise<void>;
|
|
removeItem(key: string): Promise<void>;
|
|
}
|
|
|
|
class WebStorage implements Storage {
|
|
async getItem(key: string): Promise<string | null> {
|
|
if (typeof window === "undefined") return null;
|
|
return window.localStorage.getItem(key);
|
|
}
|
|
|
|
async setItem(key: string, value: string): Promise<void> {
|
|
if (typeof window === "undefined") return;
|
|
window.localStorage.setItem(key, value);
|
|
}
|
|
|
|
async removeItem(key: string): Promise<void> {
|
|
if (typeof window === "undefined") return;
|
|
console.log(`[WebStorage] Removing key: ${key}`);
|
|
window.localStorage.removeItem(key);
|
|
console.log(`[WebStorage] Key removed. Verification:`, window.localStorage.getItem(key));
|
|
}
|
|
}
|
|
|
|
class NativeStorage implements Storage {
|
|
async getItem(key: string): Promise<string | null> {
|
|
return await SecureStore.getItemAsync(key);
|
|
}
|
|
|
|
async setItem(key: string, value: string): Promise<void> {
|
|
await SecureStore.setItemAsync(key, value);
|
|
}
|
|
|
|
async removeItem(key: string): Promise<void> {
|
|
console.log(`[NativeStorage] Removing key: ${key}`);
|
|
await SecureStore.deleteItemAsync(key);
|
|
console.log(`[NativeStorage] Key removed`);
|
|
}
|
|
}
|
|
|
|
export const storage: Storage = Platform.OS === "web" ? new WebStorage() : new NativeStorage();
|