From 8a1154522e63cb41545d9b4b980cd72d9ca702cd Mon Sep 17 00:00:00 2001 From: Leendert de Borst Date: Tue, 8 Apr 2025 17:19:58 +0200 Subject: [PATCH] Show saved credentials in react native (#771) --- mobile-app/app/(tabs)/index.tsx | 100 +++++++++++------- mobile-app/ios/CredentialManager.m | 1 + mobile-app/ios/CredentialManager.swift | 13 +++ .../ios/aliasvault/CredentialManager.swift | 23 ++++ 4 files changed, 96 insertions(+), 41 deletions(-) diff --git a/mobile-app/app/(tabs)/index.tsx b/mobile-app/app/(tabs)/index.tsx index ad7cbc46d..48b3531b3 100644 --- a/mobile-app/app/(tabs)/index.tsx +++ b/mobile-app/app/(tabs)/index.tsx @@ -1,27 +1,61 @@ -import { Image, StyleSheet, Platform, Button, View } from 'react-native'; +import { Image, StyleSheet, Platform, Button, View, FlatList, Text } from 'react-native'; import { NativeModules } from 'react-native'; +import { useEffect, useState } from 'react'; import { HelloWave } from '@/components/HelloWave'; import ParallaxScrollView from '@/components/ParallaxScrollView'; import { ThemedText } from '@/components/ThemedText'; import { ThemedView } from '@/components/ThemedView'; +interface Credential { + username: string; + password: string; + service: string; +} + export default function HomeScreen() { - const handleInsertEntry = () => { + const [credentials, setCredentials] = useState([]); + + const fetchCredentials = async () => { + try { + const result = await NativeModules.CredentialManager.getCredentials(); + setCredentials(result); + } catch (error) { + console.error('Error fetching credentials:', error); + } + }; + + useEffect(() => { + fetchCredentials(); + }, []); + + const handleInsertEntry = async () => { // Generate a random credential const randomUsername = `user${Math.floor(Math.random() * 1000)}`; const randomPassword = `pass${Math.floor(Math.random() * 1000)}`; const randomService = `service${Math.floor(Math.random() * 1000)}`; // Call native module to add credential - NativeModules.CredentialManager.addCredential(randomUsername, randomPassword, randomService); + await NativeModules.CredentialManager.addCredential(randomUsername, randomPassword, randomService); + // Add a small delay to ensure the operation is complete + await new Promise(resolve => setTimeout(resolve, 100)); + fetchCredentials(); // Refresh the list }; - const handleClearVault = () => { + const handleClearVault = async () => { // Call native module to clear credentials - NativeModules.CredentialManager.clearCredentials(); + await NativeModules.CredentialManager.clearCredentials(); + setCredentials([]); // Clear the list }; + const renderCredential = (item: Credential) => ( + + Service: {item.service} + Username: {item.username} + Password: {item.password} + + ); + return ( }> - Welcome! + AliasVault @@ -41,37 +75,12 @@ export default function HomeScreen() {