Hi when i set on a table some permission like all users read and write and then from the sdk I try to add a new row in that table I get this error: AppwriteException: The current user is not authorized to perform the requested action.
even though the row is actually added. the same happens also when i enable the row security and I specify the correct permission roles, because the row is then added or modified but i still get the error.
#bug with permissions on react native
34 messages · Page 1 of 1 (latest)
Hey! Do you have read permissions enabled? As only write & update permissions aren't enough.
If you don't have any problem, can you share the setup? i.e. the code where you've specified the permissions?
with the permission settings i tried create, read and update, but also just create and read. The thing is that the row is correctly added to the table, but i get the error anyway
So it is working but also throwing an error?
exactly, that is the weird thing
Maybe its another place throwing this error?
ERROR savePrice failed [AppwriteException: The current user is not authorized to perform the requested action.]
nope, it's definitely there
Thats kind of weird.... 😐
Try giving all permission to any
same error actually
Could you share the sdk version?
I will try to reproduce it tommorow and file a bug report if needed.
"react-native-appwrite": "^0.19.0",
"appwrite": "^21.5.0",
Why is the appwrite in your app ?
The RN one is the only thing needed
I looked at this one: https://github.com/appwrite/starter-for-react-native/blob/main/package.json
The appwrite one is not needed. Although keeping it wont cause any issues as it is a js library
Are you firstly checking if the user exists or not before making this createRow request?
with which command? i do the authentication and if i check the id of the user i see it from:
const getCurrentUserId = async (): Promise<string | null> => {
try {
const user = await account.get();
return user?.$id ?? null;
} catch (error) {
return null;
}
};
i see it, so the user is already logged in when he makes the createRow
Can you share the complete code of the file where you've this?
And this -
As per what @spiral mason is saying, it successfully works for write or update request but it fails for read. That's the problem here.
@spiral mason And one more important thing, does your API key have all the required permissions?
which api? i'm connecting with this: const client = new Client().setEndpoint(endpoint).setProject(project).setPlatform('com.example.app')
import { Query, Models, ID, Permission, Role } from 'react-native-appwrite';
import { account, tables, appwriteConfig, functions } from './appwrite';
import { AppwriteProduct, ScannedProduct, Subscription, UserProfile, FeatureRequest } from '../types/product';
const getCurrentUserId = async (): Promise<string | null> => {
try {
const user = await account.get();
return user?.$id ?? null;
} catch (error) {
return null;
}
};
import { tables, appwriteConfig } from './appwrite';
import { Query, ID, Models, Permission, Role } from 'react-native-appwrite';
import { getCurrentUserId } from './appwriteService';
export const savePrice = async (productId: string, price: number, isConfirmed = false): Promise<void> => {
const userId = await getCurrentUserId();
if (!userId) {
throw new Error('User not authenticated');
}
const normalizedPrice = Number(Number(price).toFixed(2));
const payload: Record<string, unknown> = {
product_id: productId,
price: normalizedPrice,
currency: 'EUR',
is_confirmed: isConfirmed,
user_id: userId,
recorded_at: new Date().toISOString(),
};
try {
await tables.createRow(
appwriteConfig.databaseId,
appwriteConfig.tables.prices,
ID.unique(),
payload,
);
} catch (err) {
console.error('savePrice failed', err);
throw err;
}
};
Not API. I meant The Appwrite Project's API Key
Ok. Can you try writing this part of your code -
await tables.createRow(
appwriteConfig.databaseId,
appwriteConfig.tables.prices,
ID.unique(),
payload,
);
like this -
await tables.createRow(
appwriteConfig.databaseId,
appwriteConfig.tables.prices,
ID.unique(),
payload,
[
Permission.read(Role.users()),
Permission.update(Role.user(userId)),
Permission.delete(Role.user(userId)),
]
);
I'm using the client sdk so I don't have any appwrite api key.. should i use something different?
Yes. Actually I didn't realised this before that you're using the client sdk so you don't need the api key for it, sorry about that.
Have you tried this?
Yes, just now, but the behaviour is the same, it creates the row but i get the error anyway
Did you find the same issues?
no
I tried
but could not reproduce
if you can could you share the repo privately ?
okay i found the issue, in one of the relationship tables I had the create permission disabled, so even if I didn't write in it since it was a relationship it was giving the error