Merge branch '242-password-not-working' into 'master'

Resolve "password not working"

Closes #242

See merge request bloodyhealth/drip!102
This commit is contained in:
Julia Friesel
2018-11-04 09:57:45 +00:00
3 changed files with 36 additions and 27 deletions
+4 -7
View File
@@ -24,22 +24,19 @@ export default class PasswordPrompt extends Component {
}
async tryToOpenDb() {
try {
await openDb({ persistConnection: true })
} catch (err) {
const connected = await openDb()
if (!connected) {
this.setState({ showPasswordPrompt: true })
await saveEncryptionFlag(true)
return
}
await saveEncryptionFlag(false)
this.props.showApp()
}
passHashToDb = async hash => {
try {
await openDb({ hash, persistConnection: true })
} catch (err) {
const connected = await openDb(hash)
if (!connected) {
Alert.alert(
shared.incorrectPassword,
shared.incorrectPasswordMessage,
@@ -3,10 +3,8 @@ import { openDb } from '../../../db'
import { shared } from '../../labels'
export default async function checkPassword({hash, onCancel, onTryAgain }) {
try {
await openDb({ hash, persistConnection: false })
return true
} catch (err) {
const connected = await openDb(hash)
if (connected) return true
Alert.alert(
shared.incorrectPassword,
shared.incorrectPasswordMessage,
@@ -20,4 +18,3 @@ export default async function checkPassword({hash, onCancel, onTryAgain }) {
)
return false
}
}
+18 -3
View File
@@ -10,14 +10,28 @@ let db
let isMensesStart
let getMensesDaysRightAfter
export async function openDb ({ hash, persistConnection }) {
export async function openDb (hash) {
const realmConfig = {}
if (hash) {
realmConfig.encryptionKey = hashToInt8Array(hash)
}
// perform migrations if necessary, see https://realm.io/docs/javascript/2.8.0/#migrations
// we open the db temporarily, to get the schema version even if the db is encrypted
let tempConnection
try {
tempConnection = await Realm.open(realmConfig)
} catch(err) {
// wrong password provided
if (hash && err.toString().includes('decrypt')) return false
// tried to open without password, but is encrypted
if (!hash && err.toString().includes('Invalid mnemonic')) return false
throw err
}
let nextSchemaIndex = Realm.schemaVersion(Realm.defaultPath)
tempConnection.close()
while (nextSchemaIndex < schemas.length - 1) {
const tempConfig = Object.assign(
realmConfig,
@@ -34,10 +48,11 @@ export async function openDb ({ hash, persistConnection }) {
schemas[schemas.length - 1]
))
if (persistConnection) db = connection
db = connection
const cycle = cycleModule()
isMensesStart = cycle.isMensesStart
getMensesDaysRightAfter = cycle.getMensesDaysRightAfter
return true
}
export function getBleedingDaysSortedByDate() {
@@ -212,7 +227,7 @@ export async function changeEncryptionAndRestartApp(hash) {
export async function deleteDbAndOpenNew() {
const exists = await fs.exists(Realm.defaultPath)
if (exists) await fs.unlink(Realm.defaultPath)
await openDb({ persistConnection: true })
await openDb()
}
function hashToInt8Array(hash) {