Hello, I am new to Fauna DB, and have a million noob questions, so apologies.
I want to create a site where users can register, login, and then CRUD their own documents, whilst not having access to docs created by other users.
The problem is I am struggling to make sense of user access when authenticated, and I cannot create or read any data when logged in.
This is what I have so far, which to the best of my knowledge is how to do things.
FYI I am doing this in javascript.
1 - Connect to the DB using my global secret key
import faunadb from 'faunadb';
const q = faunadb.query;
const client = new faunadb.Client({ secret: process.env.REACT_APP_FAUNA_KEY });
2 - The user logs in, and I save their secret key
.query(q.Login(q.Match(q.Index('users_by_email'), 'me@me.com'), { password: 'password' }))
.then((res) => {
userSecret = res.secret
})
3 - Create a new connection using the user’s secret key
userClient = new faunadb.Client({ secret: userSecret });
4 - Try to create a new document
userClient
.query(q.Create(q.Collection('notes'), { data: { testData: 'test data' } }))
At this point, I get a permission denied error:
Insufficient privileges to perform the action.
I understand that the user doesn’t have permission to create documents here, and I’ve been through the ABAC tutorial using the shell, but I can’t figure out how to grant permissions for a user.
Do I need to somehow pass a user id when creating a document?
Do I need to change permission on the database or collection?
And a more “best-practice” question, am I going about this the right way - i.e. would multiple users actually store their data in one single collection, or should I be creating a database or collection per user?
Thanks for your help!