import React, { useState, useEffect } from 'react'; import { View, Text, StyleSheet, Button, ScrollView, Platform, TouchableOpacity } from 'react-native'; import { Stack, useRouter } from 'expo-router'; import { X } from 'lucide-react-native'; import * as SecureStore from 'expo-secure-store'; import { SECURE_STORE_KEYS } from '@/lib/auth/constants'; import { useAuthStore } from '@/lib/auth/AuthStateManager'; import { useNDKCurrentUser } from '@/lib/hooks/useNDK'; import { migrateKeysIfNeeded, resetMigration } from '@/lib/auth/persistence/secureStorage'; /** * Test screen for debugging authentication persistence issues * This component shows the current state of authentication and stored credentials */ export default function AuthPersistenceTest() { const [storageInfo, setStorageInfo] = useState({ standardPrivateKey: 'Checking...', legacyPrivateKey: 'Checking...', ndkStoreKey: 'Checking...', pubkey: 'Checking...', externalSigner: 'Checking...', migrationStatus: 'Checking...' }); // Get auth states from both stores (legacy and React Query) const authState = useAuthStore((state) => state); const { isAuthenticated, currentUser } = useNDKCurrentUser(); const router = useRouter(); const checkStorage = async () => { try { // Check all possible storage keys const standardPrivateKey = await SecureStore.getItemAsync(SECURE_STORE_KEYS.PRIVATE_KEY); const legacyPrivateKey = await SecureStore.getItemAsync('powr.private_key'); const ndkStoreKey = await SecureStore.getItemAsync('nostr_privkey'); const pubkey = await SecureStore.getItemAsync(SECURE_STORE_KEYS.PUBKEY); const externalSigner = await SecureStore.getItemAsync(SECURE_STORE_KEYS.EXTERNAL_SIGNER); const migrationStatus = await SecureStore.getItemAsync('auth_migration_v1_completed'); // Update state with results setStorageInfo({ standardPrivateKey: standardPrivateKey ? `Found (${standardPrivateKey.length} chars)` : 'Not found', legacyPrivateKey: legacyPrivateKey ? `Found (${legacyPrivateKey.length} chars)` : 'Not found', ndkStoreKey: ndkStoreKey ? `Found (${ndkStoreKey.length} chars)` : 'Not found', pubkey: pubkey ? `Found (${pubkey.substring(0, 8)}...)` : 'Not found', externalSigner: externalSigner ? 'Found' : 'Not found', migrationStatus: migrationStatus === 'true' ? 'Completed' : 'Not run' }); } catch (error: any) { const errorMsg = error?.message || 'Unknown error'; console.error('Error checking storage:', errorMsg); setStorageInfo({ standardPrivateKey: `Error: ${errorMsg}`, legacyPrivateKey: `Error: ${errorMsg}`, ndkStoreKey: `Error: ${errorMsg}`, pubkey: `Error: ${errorMsg}`, externalSigner: `Error: ${errorMsg}`, migrationStatus: `Error: ${errorMsg}` }); } }; // Check storage when component mounts useEffect(() => { checkStorage(); }, []); return ( <> Platform.OS === 'ios' ? ( router.back()} style={{ marginLeft: 15 }}> ) : undefined, headerRight: () => Platform.OS !== 'ios' ? ( router.back()} style={{ marginRight: 15 }}> ) : undefined, }} /> Auth Persistence Debugger Zustand Auth State Status: {authState.status} {authState.status === 'authenticated' && ( <> User: {authState.user?.pubkey?.substring(0, 8)}... Method: {authState.method || 'Unknown'} )} React Query Auth State Status: {isAuthenticated ? 'authenticated' : 'unauthenticated'} {isAuthenticated && currentUser?.pubkey && ( User: {currentUser.pubkey.substring(0, 8)}... )} User Profile: {currentUser?.profile?.name || 'Not loaded'} Secure Storage Status Standard Key ({SECURE_STORE_KEYS.PRIVATE_KEY}): {storageInfo.standardPrivateKey} Legacy Key (powr.private_key): {storageInfo.legacyPrivateKey} NDK Store Key (nostr_privkey): {storageInfo.ndkStoreKey} Public Key: {storageInfo.pubkey} External Signer: {storageInfo.externalSigner} Migration Status: {storageInfo.migrationStatus}