perfood / couch-auth

Powerful authentication for APIs and apps using CouchDB (or Cloudant) with Node >= 14
MIT License
66 stars 19 forks source link

Cloudant example #48

Open fredguth opened 2 years ago

fredguth commented 2 years ago

It works with couch but I couldn't make it work with cloudant. Is there an example?

fynnlyte commented 2 years ago

Regarding your questions in #45:

A side question: what is a serverScope? I am using Cloudant and I login as admin using IAM env variables.

It's the object returned by @cloudant/cloudant when instantiating by calling with your config. Not the new IBM cloud SDK.

This package previously used @cloudant/cloudant as dependency in order to be compatible with Cloudant both with IAM and legacy authentication when setting up the _users database as described here, but I migrated to nano when the support for @cloudant/cloudant was discontinued.

I'm not sure whether Cloudant can still be used that way, but since it's in the docs it should still be possible unless you're on transaction engine. I migrated to self hosted Couch DB a while ago. If that way of using cloudant is still supported, it should work by:

If you'd want to use the new SDK with couch-auth, you'd need to provide a wrapper an object that does .request and other used DB operations the way nano does it.

It works with couch but I couldn't make it work with cloudant. Is there an example?

I don't have anything available right now.

fredguth commented 2 years ago

This was very helpful, thanks. I am not in a transaction engine AFAIK. I love to use PouchDB and CouchDB, but I just hate devops. This is the reason I was looking for Cloudant (The only "CouchDB" as a service I found).

Here is what I tried:

if (!process.env.CLOUDANT_URL || !process.env.CLOUDANT_APIKEY) {
  console.error(
    "Please create CLOUDANT_URL & CLOUDANT_APIKEY environment variables before running. See README for details."
  );
  process.exit(1);
}

const express = require("express");
const bodyParser = require("body-parser");
const { CloudantV1 } = require("@ibm-cloud/cloudant");
const client = CloudantV1.newInstance();
const { CouchAuth } = require("@perfood/couch-auth");
var config = {
  dbServer: {
    protocol: "https://",
    host: process.env.CLOUDANT_HOST,
    iamApiKey: process.env.CLOUDANT_APIKEY,
    couchAuthOnCloudant: true,
    user: process.env.CLOUDANT_USERNAME,
  },
  testMode: {
    noEmail: true,
  },
};
// constants
const PORT = 8080; // the default for Code Engine
const HOST = "0.0.0.0"; // listen on all network interfaces
const app = express();
const auth = new CouchAuth(config);

// middlewares
app.use(express.static("public"));
app.use(bodyParser.json());
app.use("/auth", auth.router);

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

client.getServerInformation().then((serverInformation) => {
  console.log(`Cloudant:${JSON.stringify(serverInformation)}`);
});

There is an error in nano.

fynnlyte commented 2 years ago

Does it work like this?:

const { CouchAuth } = require("@perfood/couch-auth");
const Cloudant = require('@cloudant/cloudant')
const serverScope = Cloudant({
      url: `https://${process.env.CLOUDANT_USER}:${process.env.CLOUDANT_PASS}@${process.env.CLOUDANT_USER}.cloudantnosqldb.appdomain.cloud`,
      plugins: [
        { iamauth: { iamApiKey: process.env.CLOUDANT_APIKEY } },
        { retry: { retryInitialDelayMsecs: 750 } }
      ],
      maxAttempt: 2
    });
const auth = new CouchAuth(config, serverScope);
fredguth commented 2 years ago

I will test this as soon as Cloudant let me in my own account. Jeez. Maybe I shouldn't be using CLoudant afterall.