#Appwrite web sdk api key
30 messages · Page 1 of 1 (latest)
setKey is only on server side sdk i.e. node-appwrite.
is there an alternative? or something
web sdk is for client use, whats your usecase with client side api use?
You really wouldn't want to expose an API on the client anyways :P
But I mean the projectId and the databaseIds are public right?
So anyone can make their own api calls for my website?
Or how should i do that?
Not if you have permissions setup appropriately
You would want people to authenticate and act on behalf of them.
Give me a minute im gonna send an example
export async function savePost(userId: string, postId: string) {
try {
const updatedPost = await databases.createDocument(
appwriteConfig.databaseID,
appwriteConfig.savesCollectionID,
ID.unique(),
{
user: userId,
post: postId,
},
[
Permission.read(Role.user(userId)),
Permission.write(Role.user(userId)),
Permission.update(Role.user(userId)),
Permission.delete(Role.user(userId)),
]
);
if (!updatedPost) throw Error;
return updatedPost;
} catch (error) {
console.log(error);
}
}
do you mean these permissions?
or this
Yea. So why would you want to use an API key, it seems to me you’re saving user posts which makes me think they’re logging in
yeah but is it safe like that?
Yes, only authenticated users can read and write
and i set the document security on because it only worked like that
i thought i needed something more like a key
No key needed. You can set document security so that only the user that created the post can edit or delete it
okay thanks, another question, if i have a collection which has user data, and there is one private data, is there a way to hide that? without functions
Like one private field?
You could create a private user data collection and only pull it when necessary
and is it a safe way for a social app? like if I want to store emails, some settings etc?
Sure just make the permissions on that collection/document such that only the user can read their own email
[
Permission.read(Role.user(userId)),
Permission.write(Role.user(userId)),
Permission.update(Role.user(userId)),
Permission.delete(Role.user(userId)),
]
kind of this right?
Yes
Thank you
@lone cairn sorry for the ping, is it safe if i only set up permissions for the getDocument? not with the create