Open magic-fruits opened 8 months ago
Hey @magic-fruits , 👋 thanks for raising this! I'm going to transfer this over to our JS repository for better assistance 🙂.
Hey there, @magic-fruits 👋. Would you mind sharing your package.json
so we can see dependencies/versions that you're using?
Hi @magic-fruits 👋 thanks for raising this issue. Can you share your GraphQL schema as well?
Amplify should be auto-populating the owner
field for you as long as you have a { allow: owner }
auth rule on the model.
For more info on owner authorization, please refer to our docs: https://docs.amplify.aws/cli-legacy/graphql-transformer/auth/#owner-authorization
@cwomack and @chrisbonifacio thank you very much for your attention. I just updated the post with the info that you suggestd: "schema" and "package.json".
The "owner" wasn't in the original schema file, but it exists since I activated the "Enable owner authorization" in Amplify Studio and then I pull the changes. This new field can be checked in the type through AWS AppSync as "owner: String" in the schema section, or in the "queries" section. I added the "owner" field to the original "graphql.schema" and from that moment I could set it manually, but if you don't set it manually it keeps being "null" when you see it in AppSync. The problem is when you try to manipulate the data inside the React App with something like:
{post.owner === user.username && (
<span style={{ color: 'red' }}> Editar</span>
)}
it just dont work, because owner is "null" if I dont set it manually and after I added it to the "graphql.schema" file. This example was shown in the AWS video, but it just doesn't work like it did in the video. I know I am missing something.
@magic-fruits can you try simply removing the owner
field from your schema? I think having it there, also as optional, is causing DataStore to expect it to be set manually.
You should only need the owner auth rule.
After removing the owner field and redeploying your app using amplify push
, try saving a new record as a logged in user and check the network activity for the graphic create mutation. Please confirm that the owner field was automatically sent in the request body, and that the request headers include an Authorization
header.
This is to confirm that DataStore is attempting to authorize the request using a Cognito access token.
Hi 👋 Closing this as we have not heard back from you. If you are still experiencing this issue and in need of assistance, please feel free to comment and provide us with any information previously requested by our team members so we can re-open this issue and be better able to assist you.
Thank you!
@chrisbonifacio
Hi !
Sorry for late response. I received the GIT notification in a newsletter folder, which I am not used to check.
I originally configurated the ", {allow: owner}" without puting the "owner: String" field in the shema. I also enabled the owner authorization in an untouched type, and didnt worked. So, This is what I have tryend:
// Create a new post
const post = new Post({
content,
blogID,
owner: user.username,
});
Before opening, please confirm:
App Id
i-use-amplify-studio-i-dont-know-which-of-all-arn-to-put-here-sorry
Region
us-east-1
Environment name
Amplify
Figma File Version (if applicable)
No response
Amplify CLI Version
No response
If applicable, what version of Node.js are you using?
No response
What operating system are you using?
No response
Browser type?
No response
Describe the bug
Hi !
How can I make "DataStore" framework insert the user.attribute.sub or user.username into the "owner" field of a GraphQL type?
This guys did that: https://youtu.be/D8gSc7wDRic?si=tmF1f7j-hr5RkJx_
I see that they only used "datastore.save" command, and as you can see, they didnt filled "owner" inside the app. Also, they make the React app compare the user.attributes.sub with the post.owner field and the app automatically recognized that they were the same.
I used that datasore.save command but the "owner" field is "null". I even tryed to set it manually, but it simply doesnt recieve the ID.
My code actually creates the post, but the "owner" field is empty. My process was: I created the Schema in my Code Editor, then I push it to Amplify Studio. It created the types in the "data" section. After that, I enabled the "Enable owner authorization" in Amplify Studio in the "Post" type.
The "Post" type has this Authorization Rules: Authenticated and unauthenticated scopes: Anyone authenticated with API Key can Create, Read, Update, and Delete Product
The "Post" type has: Owner-based scopes:
Enable owner authorization (yes, enabled) Owner-based authorization allows you to tie a data record to a user. Owners can read, create, update, and delete the record.
Allow the owner to perform these operations on their own records: Create, Read, Update, Delete
After that, the "Post" type got an additional field called "owner". Once I pulled the changes to my code editor, there schema.graphql file didnt got the "owner" field, but you can see it in AWS AppSync.
It would be wonderfull if you could help me by telling me where can I add a "console.log" to check or see what is allowing to enter the new "post" but is preventing to put the "user ID" into the "owner" field. Or you can tell me if there is a Hub function, or DataStore.suscribe or similar to identify that. It can be an option if you tell me if the error can be identified with CluodWatch or any other service that reads every answer received and answered by the server.
In the "schema" section of the AWS AppSync console, I clicked on "pipeline" of the "owner" property of the "Product" type. There is this code:
"user" is generating a big JSON object that has the next properties: Session, attributes, authenticationFlowType, client, keyPrefix, pool, preferredMFA, signInUserSession, storage, userDataKey and username", it doesn't have an "isAuthenticated" property.
It has this object (I replaced every sensitive code with "...". If there is no three points "..." its because there wasnt any code or numer)
That was a sumary of the "user" JSON object, in which I replaced every code with "..."
Would this make more clear what is blocking the fulfillment of the "owner" property? Take into account that the item is succesfully being created, with the version and time filled automatically.
This is the Schema:
This is my package.json:
This is the code of the two files, "index.js" and "App.js":
index.js
App.js
Expected behavior
The "owner" field of the GraphQL type "Post" should be filled.
Reproduction steps
Project Identifier
No response
Additional information
I created the app with the amplify command to create ReactJS apps. I am using Cloud9 The app is hosted in Amplify I am using GraphQL shcema The authetication is with Cognito, and it has activated the "users" section inside the Amplify App. Actually I dont know which arn do you need from all of that. Sorry for the answer in the arn section.