Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Recover from wrong database password #837

Closed
dlittig opened this issue Oct 8, 2018 · 1 comment
Closed

Question: Recover from wrong database password #837

dlittig opened this issue Oct 8, 2018 · 1 comment

Comments

@dlittig
Copy link

dlittig commented Oct 8, 2018

Case

Question

Issue

In my app I provide a login screen where the user enters a password to get access to the applications data. Chances are good that the user types his password wrong and tries to authenticate with it. If I try to enter a random wrong password, RxDB gives me an error like this:

Error: RxError:
RxDocument.prepare(): another instance on this adapter has a different password
Given parameters: {
passwordHash:"3676c0a1..."
existingPasswordHash:"5b3e907a3b9a95c92983a3889a5ab727"}
    at new RxError (blob:http://localhost:8081/3a48a823-f689-4dfa-a9e5-9bad378bd400:81975:129)
    at Object.newRxError (blob:http://localhost:8081/3a48a823-f689-4dfa-a9e5-9bad378bd400:82052:16)
    at RxDatabase._callee2$ (blob:http://localhost:8081/3a48a823-f689-4dfa-a9e5-9bad378bd400:70807:64)
    at tryCatch (blob:http://localhost:8081/3a48a823-f689-4dfa-a9e5-9bad378bd400:16635:19)
...


This behavior is totally fine. The interesting part comes when I try to enter the correct password, right after the previous error occurred:

Error: RxError:
RxDatabase.create(): A RxDatabase with the same name and adapter already exists.
Make sure to use this combination only once or set ignoreDuplicate to true if you do this intentional
Given parameters: {
name:"safeword"
adapter:"asyncstorage"
link:"https://pubkey.github.io/rxdb/rx-database.html#ignoreduplicate"}
    at new RxError (blob:http://localhost:8081/3a48a823-f689-4dfa-a9e5-9bad378bd400:81975:129)
    at Object.newRxError (blob:http://localhost:8081/3a48a823-f689-4dfa-a9e5-9bad378bd400:82052:16)
    at _isNameAdapterUsed (blob:http://localhost:8081/3a48a823-f689-4dfa-a9e5-9bad378bd400:71352:40)
    at Object._callee8$ (blob:http://localhost:8081/3a48a823-f689-4dfa-a9e5-9bad378bd400:70510:51)
    at tryCatch (blob:http://localhost:8081/3a48a823-f689-4dfa-a9e5-9bad378bd400:16635:19)
...

The only explanation I could think of for this error is, that an other instance of the database with the wrong password was created after the first error. Sadly the examples show only how the database is handled with a predefined password.

What helps to be able to login again is to restart the app. This leads me to the question: Is this behavior expected? Is there a way I can recover from a wrong password and afterwards open the existing database with the correct password?
This behavior occurs in React Native as well as in Electron.

Info

  • Environment: NodeJS: 10.9.0, React Native: 0.55.4, Electron: 2.0.7
  • Adapter: IndexedDB with Electron, AsyncStorage with React Native
  • Stack: Babel, React 16.5.2, RxDB 7.7.1

Code

The following function is used to connect to the database with the password provided by the user. The variable db is still empty when the error occurs as RxDB.create(rx) fails immediately.

db = null
adapter = ``
load = async (password) => {
  // Return existing database if already set up
  if(this.db !== null)
    return this.db.collection({
      name: 'safeword',
      schema
    }).catch(err => this.db.safeword)

  if(globals.PLATFORM === 'desktop') {
    RxDB.plugin(require('pouchdb-adapter-idb'))
    this.adapter = 'idb'
  } else if(globals.PLATFORM === 'mobile') {
    RxDB.plugin(require('pouchdb-adapter-asyncstorage').default)
    this.adapter = 'asyncstorage'
  }

  const rx = {
    name: 'safeword',
    adapter: this.adapter,
    multiInstance: false,
    password
  }

  return RxDB.create(rx).then(async db => {
    this.db = db
    safeword = await this.db.collection({
      name: 'safeword',
      schema
    }).catch(err => this.db.safeword)

    return this.db.safeword
  })
}
@pubkey pubkey closed this as completed in 4256c2b Oct 29, 2018
@pubkey
Copy link
Owner

pubkey commented Oct 29, 2018

@dlittig thanks for reporting. I could reproduce and fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants