import React, { Component } from 'react' import { View, TextInput, TouchableOpacity, Alert } from 'react-native' import nodejs from 'nodejs-mobile-react-native' import { AppText } from './app-text' import { hasEncryptionObservable } from '../local-storage' import styles from '../styles' import { passwordPrompt, shared } from './labels' import { openDbConnection, requestHash, deleteDbAndOpenNew, openDb } from '../db' import App from './app' export default class PasswordPrompt extends Component { constructor() { super() this.state = { password: null } hasEncryptionObservable.once((hasEncryption) => { if (hasEncryption) { this.setState({showPasswordPrompt: true}) } else { openDbConnection('something-wrong') this.setState({showApp: true}) } }) nodejs.start('main.js') nodejs.channel.addListener( 'message', this.passHashToDb, this ) } passHashToDb = async msg => { msg = JSON.parse(msg) if (msg.type != 'sha512') return try { await openDb({hash: msg.message, persistConnection: true }) this.setState({ showApp: true }) } catch (err) { Alert.alert( shared.incorrectPassword, shared.incorrectPasswordMessage, [{ text: shared.tryAgain, onPress: () => this.setState({password: null}) }] ) } } componentWillUnmount() { nodejs.channel.removeListener('message', this.passHashToDb) } render() { return ( {this.state.showApp ? : {this.state.showPasswordPrompt && this.setState({password: val})} style={styles.passwordField} /> { requestHash(this.state.password) }} > { passwordPrompt.title } { await deleteDbAndOpenNew() this.setState({showApp: true}) }} > {'Delete old db and make unencrypted new'} } } ) } }