diff --git a/components/labels.js b/components/labels.js index 15e922a48e0c8ae8cbbb2b8b719fcc98ac294336..d7f81994c06872733a1cd4d2076a463ad5dafa56 100644 --- a/components/labels.js +++ b/components/labels.js @@ -58,8 +58,8 @@ export const settings = { explainerDisabled: "Encrypt the app's database with a password. You need to enter the password every time the app is started.", explainerEnabled: "Password protection and database encryption is currently enabled", setPassword: 'Set password', - deletePassword: "Delete password", - enterCurrent: "Please enter your current password", + deletePassword: 'Remove password protection', + changePassword: 'Change password', enterNew: "Please enter a new password", backupReminderTitle: 'Have you made a backup of your data?', backupReminder: 'When you make changes to your password, we delete your old data and store it in a new version. To be safe, please backup your data using the export function before making changes to your password. Making any changes to your password setting will also restart the app immediately.', diff --git a/components/settings/password-setting.js b/components/settings/password-setting.js index be1b97406516fdf3267947e4df2400c742a21a6d..0b3a3fbc90e6bcb4a13ad68cc56d847db8c0e067 100644 --- a/components/settings/password-setting.js +++ b/components/settings/password-setting.js @@ -12,18 +12,18 @@ import { } from '../../local-storage' import styles from '../../styles/index' import { settings as labels, shared } from '../labels' -import { requestHash, openDb, changeEncryptionAndRestartApp } from '../../db' +import { requestHash, changeEncryptionAndRestartApp } from '../../db' export default class PasswordSetting extends Component { constructor(props) { super(props) this.state = { - encryptionEnabled: hasEncryptionObservable.value, - currentPassword: null, - enteringCurrentPassword: false + showUpdateAndDelete: hasEncryptionObservable.value, + showSetPassword: !hasEncryptionObservable.value, + settingNewPassword: false, + changingPassword: false } - nodejs.start('main.js') nodejs.channel.addListener( 'message', this.passHashToDb, @@ -38,41 +38,7 @@ export default class PasswordSetting extends Component { passHashToDb = async (msg) => { msg = JSON.parse(msg) if (msg.type != 'sha512') return - if (this.state.encryptionEnabled) { - await this.removeEncryption(msg.message) - } else if (!this.state.encryptionEnabled) { - await changeEncryptionAndRestartApp(msg.message) - } - } - - addEncryption = async hash => { - changeEncryptionAndRestartApp(hash) - } - - removeEncryption = async hash => { - try { - await openDb({ hash, persistConnection: false }) - } catch (err) { - console.log(err) - Alert.alert( - shared.incorrectPassword, - shared.incorrectPasswordMessage, - [{ - text: shared.cancel, - onPress: () => { - this.setState({ - enteringCurrentPassword: false, - currentPassword: null - }) - } - }, { - text: shared.tryAgain, - onPress: () => this.setState({currentPassword: null}) - }] - ) - return - } - await changeEncryptionAndRestartApp() + await changeEncryptionAndRestartApp(msg.message) } render() { @@ -81,44 +47,55 @@ export default class PasswordSetting extends Component { <AppText style={styles.settingsSegmentTitle}> {labels.passwordSettings.title} </AppText> - {this.state.encryptionEnabled ? + {this.state.showUpdateAndDelete ? <AppText>{labels.passwordSettings.explainerEnabled}</AppText> : <AppText>{labels.passwordSettings.explainerDisabled}</AppText> } - {this.state.enteringCurrentPassword && + + {this.state.showUpdateAndDelete && <View> - <TextInput - style={styles.passwordField} - onChangeText={val => { - this.setState({ - currentPassword: val, - wrongPassword: false - }) + {this.state.changingPassword && + <View> + <TextInput + style={styles.passwordField} + onChangeText={val => { + this.setState({ + changedPassword: val + }) + }} + value={this.state.changedPassword} + placeholder={labels.passwordSettings.enterNew} + secureTextEntry={true} + /> + </View> + } + <TouchableOpacity + onPress={() => { + if (!this.state.changingPassword) { + showBackUpReminder(() => { + this.setState({ changingPassword: true }) + }) + } else { + requestHash(this.state.changedPassword) + } }} - value={this.state.currentPassword} - placeholder={labels.passwordSettings.enterCurrent} - secureTextEntry={true} - /> + style={styles.settingsButton}> + <AppText style={styles.settingsButtonText}> + {labels.passwordSettings.changePassword} + </AppText> + </TouchableOpacity> + <TouchableOpacity + onPress={() => { + showBackUpReminder(() => changeEncryptionAndRestartApp()) + }} + style={styles.settingsButton}> + <AppText style={styles.settingsButtonText}> + {labels.passwordSettings.deletePassword} + </AppText> + </TouchableOpacity> </View> } - {this.state.encryptionEnabled && - <TouchableOpacity - onPress={() => { - if (!this.state.enteringCurrentPassword) { - showBackUpReminder(() => { - this.setState({ enteringCurrentPassword: true }) - }) - } else { - requestHash(this.state.currentPassword) - } - }} - style={styles.settingsButton}> - <AppText style={styles.settingsButtonText}> - {labels.passwordSettings.deletePassword} - </AppText> - </TouchableOpacity> - } {this.state.enteringNewPassword && <View> @@ -135,7 +112,7 @@ export default class PasswordSetting extends Component { /> </View> } - {!this.state.encryptionEnabled && + {this.state.showSetPassword && <TouchableOpacity onPress={() => { if (!this.state.enteringNewPassword) {