8

I wanted to try FaunaDB, so I made a NodeJS application. I followed along a tutorial which made an application like twitter. However, when I try to access database, I get 403 unauthorized message. I have checked my security keys, but I still get the same error. Any help would be appreciated.

.env file:

KEY=randomString
PORT=5000

index.js:

require("dotenv").config();
const app = require("express")();
const faunadb = require("faunadb");

const client = new faunadb.Client({
    secret: process.env.KEY,
});

const {
    Paginate,
    Get,
    Select,
    Match,
    Index,
    Create,
    Collection,
    Lambda,
    Var,
    Join,
    Ref,
} = faunadb.query;

app.listen(5000, () => console.log(`API on http://localhost:${process.env.PORT}`));

app.get("/tweet/:id", async (req, res) => {
    try {
        const doc = await client.query(
            Get(
                Ref(
                    Collection("tweets"),
                    req.params.id
                )
            )
        )
        res.send(doc);

    } catch (err) {
        res.send(err)
    }
});

Error message:

HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 853
ETag: W/"355-EPYXYAwyDrJxa8vWUbY5JYPY+pw"
Date: Thu, 22 Jul 2021 11:12:16 GMT
Connection: close

{
  "name": "Unauthorized",
  "message": "unauthorized",
  "description": "Unauthorized",
  "requestResult": {
    "method": "POST",
    "path": "",
    "query": null,
    "requestRaw": "{\"create\":{\"collection\":\"test\"},\"params\":{\"object\":{\"data\":{\"object\":{\"testField\":\"testValue\"}}}}}",
    "requestContent": {
      "create": {
        "collection": "test"
      },
      "params": {
        "object": {
          "data": {
            "object": {
              "testField": "testValue"
            }
          }
        }
      }
    },
    "responseRaw": "{\"errors\":[{\"code\":\"unauthorized\",\"description\":\"Unauthorized\"}]}",
    "responseContent": {
      "errors": [
        {
          "code": "unauthorized",
          "description": "Unauthorized"
        }
      ]
    },
    "statusCode": 401,
    "responseHeaders": {
      ":status": 401,
      "www-authenticate": "Basic realm=\"Unauthorized\"",
      "x-txn-time": "1626952335964976",
      "x-faunadb-build": "070821.200951-e596d0a",
      "content-length": "65",
      "content-type": "application/json;charset=utf-8"
    },
    "startTime": 1626952335231,
    "endTime": 1626952336270
  }
}
Azzarian
  • 153
  • 2
  • 13
  • 2
    Have you tried to add a schema and domain params to the client instantiation? `const client = new faunadb.Client({ domain: 'db.fauna.com', scheme: 'https', secret: process.env.KEY });` – Stanislav Parkhomenko Jul 22 '21 at 13:37
  • 3
    Stanislav is on to something important, if you selected either the US or EU region group for your database, then you need to specify the domain parameter to your client. If the domain does not match, then you get a simple 401 error. https://docs.fauna.com/fauna/current/api/fql/region_groups – ptpaterson Jul 22 '21 at 13:59
  • You are right! I added the parameters and added the domain for the region I specified in the Fauna dashboard (mine was EU so the domain was db.eu.fauna.com). Thank you so much! You should put this as an answer. – Azzarian Jul 22 '21 at 15:12

1 Answers1

13

Instantiating the client like this:

const client = new faunadb.Client({
  secret: process.env.KEY,
});

you are applying a few default parameters, as if you write your code this way (I'm specifying the most important ones only):

const client = new faunadb.Client({
  secret: process.env.KEY,
  domain: 'db.fauna.com',
  scheme: 'https',
});

In case you are using US Region group, EU Region group, or Preview environment, "db.fauna.com" default domain won't work for you.

Thus, you need to provide a domain param explicitly in the constructor.

For US Region group:

const client = new faunadb.Client({
  secret: process.env.KEY,
  domain: 'db.us.fauna.com',
  scheme: 'https',
});

For EU Region group:

const client = new faunadb.Client({
  secret: process.env.KEY,
  domain: 'db.eu.fauna.com',
  scheme: 'https',
});

For Preview:

const client = new faunadb.Client({
  secret: process.env.KEY,
  domain: 'db.fauna-preview.com',
  scheme: 'https',
});

You can read more about Region Groups in the docs: https://docs.fauna.com/fauna/current/api/fql/region_groups