Add password setting and check password before deleting it

This commit is contained in:
Julia Friesel
2018-09-13 20:42:24 +02:00
parent 88fc3cad09
commit b153f8d0ff
5 changed files with 144 additions and 29 deletions
+85
View File
@@ -0,0 +1,85 @@
import React, { Component } from 'react'
import {
View,
TouchableOpacity,
TextInput
} from 'react-native'
import nodejs from 'nodejs-mobile-react-native'
import { AppText } from '../app-text'
import {
hasEncryptionObservable
} from '../../local-storage'
import styles from '../../styles/index'
import { settings as labels } from '../labels'
import { requestHash, openDb } from '../../db'
export default class PasswordSetting extends Component {
constructor(props) {
super(props)
this.state = {
enabled: hasEncryptionObservable.value
}
nodejs.start('main.js')
nodejs.channel.addListener(
'message',
this.passHashToDb,
this
)
}
componentWillUnmount() {
nodejs.channel.removeListener('message', this.passHashToDb)
}
passHashToDb = async (msg) => {
msg = JSON.parse(msg)
if (msg.type != 'sha512') return
try {
await openDb({ hash: msg.message, persistConnection: false })
this.setState({
wrongPassword: false,
enterOldPassword: false
})
} catch (err) {
this.setState({wrongPassword: true})
}
}
render() {
return (
<View style={styles.settingsSegment}>
<AppText style={styles.settingsSegmentTitle}>
{labels.passwordSettings.title}
</AppText>
{this.state.enabled ?
<AppText>{labels.passwordSettings.explainerEnabled}</AppText>
:
<AppText>{labels.passwordSettings.explainerDisabled}</AppText>
}
{this.state.enterOldPassword &&
<TextInput
style={{
backgroundColor: 'white',
}}
onChangeText={val => this.setState({ oldPassword: val })}
/>
}
{this.state.wrongPassword && <AppText>Wrong PAssword!</AppText>}
<TouchableOpacity
onPress={() => {
if (!this.state.enterOldPassword) {
this.setState({ enterOldPassword: true })
} else {
requestHash(this.state.oldPassword)
}
}}
style={styles.settingsButton}>
<AppText style={styles.settingsButtonText}>
{labels.passwordSettings.deletePassword}
</AppText>
</TouchableOpacity>
</View>
)
}
}