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:
@@ -24,22 +24,19 @@ export default class PasswordPrompt extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async tryToOpenDb() {
|
async tryToOpenDb() {
|
||||||
try {
|
const connected = await openDb()
|
||||||
await openDb({ persistConnection: true })
|
if (!connected) {
|
||||||
} catch (err) {
|
|
||||||
this.setState({ showPasswordPrompt: true })
|
this.setState({ showPasswordPrompt: true })
|
||||||
await saveEncryptionFlag(true)
|
await saveEncryptionFlag(true)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
await saveEncryptionFlag(false)
|
await saveEncryptionFlag(false)
|
||||||
this.props.showApp()
|
this.props.showApp()
|
||||||
}
|
}
|
||||||
|
|
||||||
passHashToDb = async hash => {
|
passHashToDb = async hash => {
|
||||||
try {
|
const connected = await openDb(hash)
|
||||||
await openDb({ hash, persistConnection: true })
|
if (!connected) {
|
||||||
} catch (err) {
|
|
||||||
Alert.alert(
|
Alert.alert(
|
||||||
shared.incorrectPassword,
|
shared.incorrectPassword,
|
||||||
shared.incorrectPasswordMessage,
|
shared.incorrectPasswordMessage,
|
||||||
|
|||||||
@@ -3,21 +3,18 @@ import { openDb } from '../../../db'
|
|||||||
import { shared } from '../../labels'
|
import { shared } from '../../labels'
|
||||||
|
|
||||||
export default async function checkPassword({hash, onCancel, onTryAgain }) {
|
export default async function checkPassword({hash, onCancel, onTryAgain }) {
|
||||||
try {
|
const connected = await openDb(hash)
|
||||||
await openDb({ hash, persistConnection: false })
|
if (connected) return true
|
||||||
return true
|
Alert.alert(
|
||||||
} catch (err) {
|
shared.incorrectPassword,
|
||||||
Alert.alert(
|
shared.incorrectPasswordMessage,
|
||||||
shared.incorrectPassword,
|
[{
|
||||||
shared.incorrectPasswordMessage,
|
text: shared.cancel,
|
||||||
[{
|
onPress: onCancel
|
||||||
text: shared.cancel,
|
}, {
|
||||||
onPress: onCancel
|
text: shared.tryAgain,
|
||||||
}, {
|
onPress: onTryAgain
|
||||||
text: shared.tryAgain,
|
}]
|
||||||
onPress: onTryAgain
|
)
|
||||||
}]
|
return false
|
||||||
)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
+18
-3
@@ -10,14 +10,28 @@ let db
|
|||||||
let isMensesStart
|
let isMensesStart
|
||||||
let getMensesDaysRightAfter
|
let getMensesDaysRightAfter
|
||||||
|
|
||||||
export async function openDb ({ hash, persistConnection }) {
|
export async function openDb (hash) {
|
||||||
const realmConfig = {}
|
const realmConfig = {}
|
||||||
if (hash) {
|
if (hash) {
|
||||||
realmConfig.encryptionKey = hashToInt8Array(hash)
|
realmConfig.encryptionKey = hashToInt8Array(hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
// perform migrations if necessary, see https://realm.io/docs/javascript/2.8.0/#migrations
|
// 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)
|
let nextSchemaIndex = Realm.schemaVersion(Realm.defaultPath)
|
||||||
|
tempConnection.close()
|
||||||
while (nextSchemaIndex < schemas.length - 1) {
|
while (nextSchemaIndex < schemas.length - 1) {
|
||||||
const tempConfig = Object.assign(
|
const tempConfig = Object.assign(
|
||||||
realmConfig,
|
realmConfig,
|
||||||
@@ -34,10 +48,11 @@ export async function openDb ({ hash, persistConnection }) {
|
|||||||
schemas[schemas.length - 1]
|
schemas[schemas.length - 1]
|
||||||
))
|
))
|
||||||
|
|
||||||
if (persistConnection) db = connection
|
db = connection
|
||||||
const cycle = cycleModule()
|
const cycle = cycleModule()
|
||||||
isMensesStart = cycle.isMensesStart
|
isMensesStart = cycle.isMensesStart
|
||||||
getMensesDaysRightAfter = cycle.getMensesDaysRightAfter
|
getMensesDaysRightAfter = cycle.getMensesDaysRightAfter
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getBleedingDaysSortedByDate() {
|
export function getBleedingDaysSortedByDate() {
|
||||||
@@ -212,7 +227,7 @@ export async function changeEncryptionAndRestartApp(hash) {
|
|||||||
export async function deleteDbAndOpenNew() {
|
export async function deleteDbAndOpenNew() {
|
||||||
const exists = await fs.exists(Realm.defaultPath)
|
const exists = await fs.exists(Realm.defaultPath)
|
||||||
if (exists) await fs.unlink(Realm.defaultPath)
|
if (exists) await fs.unlink(Realm.defaultPath)
|
||||||
await openDb({ persistConnection: true })
|
await openDb()
|
||||||
}
|
}
|
||||||
|
|
||||||
function hashToInt8Array(hash) {
|
function hashToInt8Array(hash) {
|
||||||
|
|||||||
Reference in New Issue
Block a user