Only Update Data fails with error: "Unauthorized on [owner]" but owner is defined to be allowed to CRUD on resource.ts #5834
Labels
pending-triage
This issue is in the backlog of issues to triage
Description
**Summary: **
For data with fields and authorizations defined as follows, create, read, and delete succeed, but only update fails.
DeviceTokenData: a
.model({
id: a.id().required(),
owner: a.string().authorization((allow) => [allow.owner().to(['read', 'delete'])]),
deviceToken: a.string().required(),
})
.secondaryIndexes((index) => [
index('owner')
])
.authorization((allow) => [allow.owner()]),
Background:
We built a data model to store device tokens for sending push notifications.
To define a secondary index on the owner, we defined an “owner” field that does not need to be explicitly defined, and only for this field we defined a field-level authorization for this field only at the field level.
Categories
Steps to Reproduce
DeviceTokenData: a
.model({
id: a.id().required(),
owner: a.string().authorization((allow) => [allow.owner().to(['read', 'delete'])]),
deviceToken: a.string().required(),
})
.secondaryIndexes((index) => [
index('owner')
])
.authorization((allow) => [allow.owner()]),
// get record first
final queryRequest = ModelQueries.get(
DeviceTokenData.classType,
DeviceTokenDataModelIdentifier(id: deviceId),
);
final queryResponse = await Amplify.API.query(request: queryRequest).response;
// then update
final newDeviceTokenData = queryResponse.data!.copyWith(deviceToken: deviceToken);
final updateRequest = ModelMutations.update(
newDeviceTokenData
);
final updateResponse = await Amplify.API.mutate(request: updateRequest).response;
if (updateResponse.errors.isNotEmpty) {
logger.e('Update failed: ${updateResponse.errors.map((e) => 'Message: ${e.message}, ErrorType: ${e.errorType}').join('\n')}');
return;
}
Screenshots
No response
Platforms
Flutter Version
3.24.4
Amplify Flutter Version
2.5.0
Deployment Method
Amplify Gen 2
Schema
The text was updated successfully, but these errors were encountered: