Closed CooperW824 closed 4 months ago
Hi @CooperW824 can you try setting the auth mode to userPool
in your request? You can change between userPool
and identityPool
depending on whether your user is in a group or not:
const listTodos = async () => {
const session = await fetchAuthSession();
let groups = session.tokens?.accessToken.payload["cognito:groups"];
console.log({ groups });
const { data, errors } = await client.models.Todo.list({
authMode: groups ? "userPool" : "identityPool",
});
if (errors) {
console.error({ errors });
}
console.log({ data });
};
We're working on a feature that will solve this automatically but for now this would be the suggested workaround. Marking this as a feature-request for tracking along with the work.
Thanks for reaching out! Yeah using the userPool
auth mode works fine. This work around will work for what I need to do here. Thanks to the Amplify team for all the great work!
Before opening, please confirm:
JavaScript Framework
React
Amplify APIs
Authentication, GraphQL API
Amplify Version
v6
Amplify Categories
auth, api
Backend
Amplify Gen 2 (Preview)
Environment information
Describe the bug
So I define my API like so with the goal of only admins being able to modify content, and with authenticated and unauthenticated users being able to read the content using the Identity Pool. However, if an Admin user tries to use the identity pool to read the data, they get a "GraphQL: Unauthorized" error. All other users (authenticated or not) can read, as expected, but users with this 'admin' user group can't read using the Identity Pool.
API definition:
Expected behavior
I would expect, that since the admin user is authenticated, they can use the Identity Pool to read the data from the API without any errors.
Reproduction steps
Code Snippet
I was able to reproduce the bug in this example repository: https://github.com/CooperW824/amplify-user-group-auth-issue-example
amplify/data/resource.ts
app/page.tsx
Log output
aws-exports.js
No response
Manual configuration
No response
Additional configuration
No response
Mobile Device
No response
Mobile Operating System
No response
Mobile Browser
No response
Mobile Browser Version
No response
Additional information and screenshots
No response