Hi!
I’m new to fauna and I’m trying to connect my node.js project to the DB. But I keep getting unauthorized error, the one below.
Unauthorized: unauthorized. Check that endpoint, schema, port and secret are correct during client’s instantiation
I’ve checked some of the earlier posts in here with the same topic and tried changing the domain but so far with no effect. The secret key should be valid. I’m not quite sure how to proceed from here to make it work.
Can you help?
Below is the code snippet from my project
import faunadb from "faunadb";
const q = faunadb.query;
import dotenv from "dotenv";
dotenv.config();
const client = new faunadb.Client({ secret: process.env.FAUNADBKEY,
domain: 'db.eu.fauna.com' });
export async function createUser(username, email, hashedPassword) {
const result = await client.query(
q.Create(q.Collection("Users"), {
data: { username, email, password: hashedPassword },
})
);
return result;
}
That looks like a 401 message, which indicates that the secret is either missing or invalid.
Providing a specialized domain is no longer necessary with our latest intelligent routing. All cloud queries can be directed to db.fauna.com. Previously sending a request to the wrong Region Group was similar to providing an invalid key (a key for a different RG), so a 401 was returned; however, this is not the case anymore. We will need to tidy up some previous posts if we still point to that as a solution for 401 errors.
First, please make sure that your environment variable is actually being imported. You want to be sure your .env file is set up correctly and in the write directory. Since you are using import syntax, you should use dotenv per the README instructions.
import 'dotenv/config'
If the secret is being imported correctly, and you are still receiving the 401 error, then make sure that the key is still active. You can also test with an admin key temporarily if necessary.
Curious if you’re following a tutorial from somewhere and if so, if you could point us to it. The code you’re sharing is using an old version of our query language (which is still active; and couldn’t be the cause of your 401 errors). The new language is much improved and quite different than the old one, so it may be a better idea to use updated examples and tutorials. A good place to get started with Fauna is this page.