amplify-swift: App Sync throwing "Not Authorized" for some fields
Describe the bug
When I run my app datastore attempts to sync my models data, but on 2 of my models some fields are returning as “Not Authorized to access member on type <Model>”. We are using the same infrastructure to web and iOS clients, and just on iOS I’m having this authentication issue when accessing models.
Here’s how I configure my Amplify:
do {
let apiPlugin = AWSAPIPlugin(modelRegistration: AmplifyModels())
try Amplify.add(plugin: AWSDataStorePlugin(modelRegistration: AmplifyModels()))
try Amplify.add(plugin: AWSCognitoAuthPlugin())
try Amplify.add(plugin: AWSS3StoragePlugin())
try Amplify.add(plugin: apiPlugin)
try Amplify.configure()
// I'm using a custom interceptor, bc without it I was receiving 401 when performing requests to the REST API
try apiPlugin.add(interceptor: CustomInterceptor(), for: AmplifyEndpoints.REST_API)
print("Initialized Amplify")
} catch {
print("Could not initialize Amplify: \(error)")
}
struct CustomInterceptor: URLRequestInterceptor {
func intercept(_ request: URLRequest) async throws -> URLRequest {
var request = request
request.setValue(try await getAccessToken(), forHTTPHeaderField: "Authorization")
return request
}
}
Steps To Reproduce
Open the app with an authenticated session
Expected behavior
Be able to sync DataStore without errors
Amplify Framework Version
2.15.2
Amplify Categories
DataStore
Dependency manager
Swift PM
Swift version
5.8.1
CLI version
12.1.1
Xcode version
Xcode 14.3.1 (14E300b)
Relevant log output
<details>
<summary>Log Messages</summary>
-08-16 12:22:24.544276-0300 AppName[5835:2632035] [DataStoreRemoteSyncEngine] One or more errors occurred syncing models. See below for detailed error description.
2023-08-16 12:22:24.544458-0300 AppName[5835:2632035] [DataStoreRemoteSyncEngine] DataStoreError: An error occurred syncing Connection
Caused by:
GraphQLResponseError<PaginatedList<AnyModel>>: GraphQL service returned a partially-successful response containing errors: [Amplify.GraphQLError(message: "Not Authorized to access member on type Connection", locations: Optional([Amplify.GraphQLError.Location(line: 23, column: 7)]), path: Optional([Amplify.JSONValue.string("syncConnections"), Amplify.JSONValue.string("items"), Amplify.JSONValue.number(2.0), Amplify.JSONValue.string("member")]), extensions: Optional(["errorInfo": Amplify.JSONValue.null, "data": Amplify.JSONValue.null, "errorType": Amplify.JSONValue.string("Unauthorized")])), Amplify.GraphQLError(message: "Not Authorized to access member on type Connection", locations: Optional([Amplify.GraphQLError.Location(line: 23, column: 7)]), path: Optional([Amplify.JSONValue.string("syncConnections"), Amplify.JSONValue.string("items"), Amplify.JSONValue.number(0.0), Amplify.JSONValue.string("member")]), extensions: Optional(["errorType": Amplify.JSONValue.string("Unauthorized"), "data": Amplify.JSONValue.null, "errorInfo": Amplify.JSONValue.null])), Amplify.GraphQLError(message: "Not Authorized to access member on type Connection", locations: Optional([Amplify.GraphQLError.Location(line: 23, column: 7)]), path: Optional([Amplify.JSONValue.string("syncConnections"), Amplify.JSONValue.string("items"), Amplify.JSONValue.number(4.0), Amplify.JSONValue.string("member")]), extensions: Optional(["data": Amplify.JSONValue.null, "errorInfo": Amplify.JSONValue.null, "errorType": Amplify.JSONValue.string("Unauthorized")])), Amplify.GraphQLError(message: "Not Authorized to access member on type Connection", locations: Optional([Amplify.GraphQLError.Location(line: 23, column: 7)]), path: Optional([Amplify.JSONValue.string("syncConnections"), Amplify.JSONValue.string("items"), Amplify.JSONValue.number(1.0), Amplify.JSONValue.string("member")]), extensions: Optional(["errorType": Amplify.JSONValue.string("Unauthorized"), "data": Amplify.JSONValue.null, "errorInfo": Amplify.JSONValue.null]))]
Recovery suggestion: The list of `GraphQLError` contains service-specific messages.
DataStoreError: An error occurred syncing Participant
Caused by:
DataStoreError: GraphQL service returned a partially-successful response containing errors: [Amplify.GraphQLError(message: "Not Authorized to access room on type Participant", locations: Optional([Amplify.GraphQLError.Location(line: 29, column: 7)]), path: Optional([Amplify.JSONValue.string("syncParticipants"), Amplify.JSONValue.string("items"), Amplify.JSONValue.number(0.0), Amplify.JSONValue.string("room")]), extensions: Optional(["errorType": Amplify.JSONValue.string("Unauthorized"), "data": Amplify.JSONValue.null, "errorInfo": Amplify.JSONValue.null])), Amplify.GraphQLError(message: "Not Authorized to access room on type Participant", locations: Optional([Amplify.GraphQLError.Location(line: 29, column: 7)]), path: Optional([Amplify.JSONValue.string("syncParticipants"), Amplify.JSONValue.string("items"), Amplify.JSONValue.number(1.0), Amplify.JSONValue.string("room")]), extensions: Optional(["data": Amplify.JSONValue.null, "errorType": Amplify.JSONValue.string("Unauthorized"), "errorInfo": Amplify.JSONValue.null]))]
Recovery suggestion: The list of `GraphQLError` contains service-specific messages.
</details>
Is this a regression?
No
Regression additional context
No response
Platforms
iOS
OS Version
iOS 16.6
Device
iPhone 13
Specific to simulators
No
Additional context
Here’s the models involved in the bug
type User
@model
@auth(rules: [# { allow: private, operations: [sync, listen] } { allow: owner, operations: [read] }]) {
id: ID
owner: ID
username: String
email: String
name: String
givenName: String
familyName: String
picture: String
searchTerm: String
createdAt: AWSDateTime
updatedAt: AWSDateTime
}
type Room @model @auth(rules: [{ allow: owner }]) {
id: ID
owner: ID
userId: ID @index(name: "listRoomByUserId", queryField: "listRoomByUserId", sortKeyFields: ["createdAt"])
teamId: ID @index(name: "listRoomByTeamId", queryField: "listRoomByTeamId", sortKeyFields: ["createdAt"])
name: String
isPrivate: Boolean
roomKey: String
isGroup: Boolean
photoKey: String
photoUrl: String
searchTerm: String
createdAt: AWSDateTime
updatedAt: AWSDateTime
}
type Connection @model @auth(rules: [{ allow: owner }]) {
id: ID
owner: ID
chatId: ID
@index(
name: "listConnectionByChatId"
queryField: "listConnectionByChatId"
sortKeyFields: ["createdAt"]
)
userId: ID
@index(
name: "listConnectionByUserId"
queryField: "listConnectionByUserId"
sortKeyFields: ["createdAt"]
)
memberId: ID
@index(
name: "listConnectionByMemberId"
queryField: "listConnectionByMemberId"
sortKeyFields: ["createdAt"]
)
user: User @hasOne(fields: ["userId"])
member: User @hasOne(fields: ["memberId"])
onlinePresence: OnlinePresence @hasOne(fields: ["memberId"])
isSender: Boolean
isReceiver: Boolean
isAccepted: Boolean
isDeclined: Boolean
isBlocked: Boolean
isMuted: Boolean
isPinned: Boolean
acceptedAt: AWSDateTime
declinedAt: AWSDateTime
blockedAt: AWSDateTime
mutedAt: AWSDateTime
pinnedAt: AWSDateTime
createdAt: AWSDateTime
updatedAt: AWSDateTime
}
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 1
- Comments: 16 (7 by maintainers)
Hey folks, any updates on this issue?
I accidentally closed this issue while I was formatting these comment. Sorry about that. Here are the changes related to the Connection+Schema and Connection:
Connection+Schema diff
Connection diff
Hi @lawmicha, I’ve added the feature flag to
cli.json, and regenerated my models, but DataStore still having errors when it’s trying to sync with Connections and Participants.DataStore Logs
Hey, are there any updates on your investigation, and what’s the average time it usually takes to receive updates?
After further investigation, I noticed the web project is performing a query with fewer fields than the iOS does:
Query from web project
Query from iOS project
But the same query that’s defined at
myProjectWeb/amplify/backend/function/myProjectLambda/src/gqlshows the same structure as the iOS query.Query from lambda folder
Testing both queries on AppSync console with an authenticated user, I managed to get the data using the web query, while the iOS query returned unauthorized errors as the DataStore sync errors I sent before.
So I believe that’s why I don’t get the iOS errors on web, but the question I raise is: There’s a way to modify the query structure on iOS? Or maybe modify it on backend?
Thanks for opening this issue.
The team will investigate and get back to you as soon as we have more information.