Skip to content

Commit

Permalink
Fix reference to many Cognito users (#39)
Browse files Browse the repository at this point in the history
* Fix reference to many Cognito users

* Bump version

* Update README

* Add FUNDING
  • Loading branch information
MrHertal authored Feb 19, 2021
1 parent ca1689d commit 8000701
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# These are supported funding model platforms

github: [MrHertal] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ An additional prop `storageOptions` is available and is passed to [Storage.get](

First [configure admin queries](https://docs.amplify.aws/cli/auth/admin#enable-admin-queries) in your Amplify project.

Don't forget to update the configuration file `aws-exports.js` if it was imported from another project.

Then you have to set the data provider option `enableAdminQueries`:

```jsx
Expand Down Expand Up @@ -567,3 +569,7 @@ export default App;
```

`CognitoUserList`, `CognitoUserShow` and `CognitoGroupList` are provided by this library to help you quickly setting things up. You can replace them by your own components if you want to add some customizations.

## License

MIT
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-admin-amplify",
"version": "1.3.0",
"version": "1.3.1",
"description": "AWS Amplify data provider for react-admin.",
"repository": {
"type": "git",
Expand Down
28 changes: 28 additions & 0 deletions src/providers/AdminQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Auth } from "@aws-amplify/auth";
import {
GetListParams,
GetListResult,
GetManyParams,
GetManyResult,
GetOneParams,
GetOneResult,
} from "ra-core";
Expand Down Expand Up @@ -181,6 +183,7 @@ export class AdminQueries {
static async getCognitoUser<RecordType>(
params: GetOneParams
): Promise<GetOneResult<RecordType>> {
// Executes the query
const userData = await AdminQueries.get("/getUser", {
username: params.id,
});
Expand All @@ -192,6 +195,31 @@ export class AdminQueries {
};
}

static async getManyCognitoUsers<RecordType>(
params: GetManyParams
): Promise<GetManyResult<RecordType>> {
const users = [];

// Executes the queries
for (const id of params.ids) {
try {
const userData = await AdminQueries.get("/getUser", {
username: id,
});

const user = AdminQueries.parseUser(userData);

users.push(user);
} catch (e) {
console.log(e);
}
}

return {
data: users,
};
}

static parseUser(user: any) {
const { Username, Attributes, UserAttributes, ...fields } = user;

Expand Down
4 changes: 4 additions & 0 deletions src/providers/DataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ export class DataProvider {
resource: string,
params: GetManyParams
): Promise<GetManyResult<RecordType>> => {
if (this.enableAdminQueries && resource === "cognitoUsers") {
return AdminQueries.getManyCognitoUsers(params);
}

const queryName = this.getQueryName("get", resource);
const query = this.getQuery(queryName);

Expand Down

0 comments on commit 8000701

Please sign in to comment.