From 80f08c0642bce62b5d818855b7a3df7171e46d47 Mon Sep 17 00:00:00 2001
From: Julia Friesel <julia.friesel@gmail.com>
Date: Sat, 15 Sep 2018 10:34:46 +0200
Subject: [PATCH] Don't rely on local storage knowing about encryption state

---
 components/password-prompt.js | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/components/password-prompt.js b/components/password-prompt.js
index 78d1b93e..6ff59041 100644
--- a/components/password-prompt.js
+++ b/components/password-prompt.js
@@ -1,8 +1,8 @@
 import React, { Component } from 'react'
 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 { hasEncryptionObservable } from '../local-storage'
 import styles from '../styles'
 import { passwordPrompt as labels, shared } from './labels'
 import { requestHash, deleteDbAndOpenNew, openDb } from '../db'
@@ -13,21 +13,25 @@ export default class PasswordPrompt extends Component {
     this.state = {
       password: null
     }
-    hasEncryptionObservable.once(async hasEncryption => {
-      hasEncryption = JSON.parse(hasEncryption)
-      if (hasEncryption) {
-        this.setState({showPasswordPrompt: true})
-      } else {
-        await openDb({persistConnection: true})
-        this.props.showApp()
-      }
-    })
 
     nodejs.channel.addListener(
       'message',
       this.passHashToDb,
       this
     )
+
+    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 => {
-- 
GitLab