3👍
✅
As you have Legacy Auth available, I won’t worry about IAM authentication. We can simply generate an API key/password for your mobile client and use legacy authentication.
Creating an API key
In the Cloudant Dashboard, choose the "Permissions" menu and click the "Generate API Key" button. You should see a generated Key and Password – these become the Username & Password in the URL you feed to PouchDB.
If you’re doing a sync (i.e data is to flow in both directions) then your API key needs both _reader
& _writer
permissions. If data is to flow from server to mobile only, then _reader
& _replicator
will suffice.
Client-side code
Your client side code is pretty much correct:
- remember that Cloudant only accepts HTTPS connections
- the Key from your API Key/Password pair becomes your username
- the Password from your API Key/Password pair becomes your password
e.g.
const PouchDB = require('pouchdb')
const db = new PouchDB('todos')
const username = 'KEY'
const password = 'PASSWORD'
const host = 'mycloudservice.cloudant.com'
const databaseName = 'todos'
const remoteDB = `https://${username}:${password}@${host}/${databaseName}`
db.sync(remoteDB).on('complete', async function () {
console.log('done')
}).on('error', function (err) {
console.error(err)
});
Source:stackexchange.com