Skip to content
Snippets Groups Projects
password-prompt.js 3.3 KiB
Newer Older
import React, { Component } from 'react'
Julia Friesel's avatar
Julia Friesel committed
import { View, TextInput, TouchableOpacity, Alert, Image } from 'react-native'
import nodejs from 'nodejs-mobile-react-native'
import { saveEncryptionFlag } from '../local-storage'
import { AppText } from './app-text'
import styles from '../styles'
Julia Friesel's avatar
Julia Friesel committed
import { passwordPrompt as labels, shared } from './labels'
import { requestHash, deleteDbAndOpenNew, openDb } from '../db'

export default class PasswordPrompt extends Component {
  constructor(props) {
    super(props)
    this.state = {
      password: null
    }
    nodejs.channel.addListener(
      'message',

    this.tryToOpenDb()
  }

  async tryToOpenDb() {
    try {
      await openDb({ persistConnection: true })
      await saveEncryptionFlag(false)
    } catch (err) {
      this.setState({ showPasswordPrompt: true })
      await saveEncryptionFlag(true)
      return
    }
  passHashToDb = async msg => {
    msg = JSON.parse(msg)
    if (msg.type != 'sha512') return
    try {
      await openDb({hash: msg.message, persistConnection: true })
      Alert.alert(
        shared.incorrectPassword,
        shared.incorrectPasswordMessage,
        [{
          text: shared.tryAgain,
          onPress: () => this.setState({password: null})
        }]
      )
Julia Friesel's avatar
Julia Friesel committed
    this.props.showApp()
Julia Friesel's avatar
Julia Friesel committed
  confirmDeletion = async () => {
    Alert.alert(
      labels.deleteDatabaseTitle,
      labels.deleteDatabaseExplainer,
      [{
        text: shared.cancel,
        style: 'cancel'
      }, {
        text: labels.deleteData,
        onPress: () => {
          Alert.alert(
            labels.areYouSureTitle,
            labels.areYouSure,
            [{
              text: shared.cancel,
              style: 'cancel'
            }, {
              text: labels.reallyDeleteData,
              onPress: async () => {
                await deleteDbAndOpenNew()
                this.props.showApp()
              }
            }]
          )
        }
      }]
    )
  }

  componentWillUnmount() {
    nodejs.channel.removeListener('message', this.passHashToDb)
Julia Friesel's avatar
Julia Friesel committed
      <View flex={1}>
        {this.state.showPasswordPrompt &&
Julia Friesel's avatar
Julia Friesel committed
          <View style={styles.passwordPromptPage}>
            <Image
              source={require('../assets/drip_small.png')}
              style={styles.passwordPromptImage}
            />
            <TextInput
              onChangeText={val => this.setState({ password: val })}
Julia Friesel's avatar
Julia Friesel committed
              style={styles.passwordPromptField}
Julia Friesel's avatar
Julia Friesel committed
              secureTextEntry={true}
Julia Friesel's avatar
Julia Friesel committed
              placeholder={labels.enterPassword}
Julia Friesel's avatar
Julia Friesel committed
              style={styles.passwordPromptButton}
              onPress={() => {
                requestHash(this.state.password)
              }}
Julia Friesel's avatar
Julia Friesel committed
              disabled={!this.state.password}
Julia Friesel's avatar
Julia Friesel committed
              <AppText style={styles.passwordPromptButtonText}>
Julia Friesel's avatar
Julia Friesel committed
                {labels.title}
              </AppText>
            </TouchableOpacity>
            <TouchableOpacity
Julia Friesel's avatar
Julia Friesel committed
              onPress={this.confirmDeletion}
Julia Friesel's avatar
Julia Friesel committed
              <AppText style={styles.passwordPromptForgotPasswordText}>
                {labels.forgotPassword}
              </AppText>
            </TouchableOpacity>