amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

Quick Question - AdminQueries documentation and examples?

Open xitanggg opened this issue 6 years ago • 4 comments

Which Category is your question related to? Amplify Cognito

What AWS Services are you utilizing? Amplify CLI Function Admin Queries

Amplify CLI comes with the function AdminQueries. It is a powerful tool to list users, add user to group and do all sorts of Cognito stuff. However, I struggle to find a complete documentation and examples. Is there a complete documentation and examples somewhere? Thanks

xitanggg avatar Apr 13 '20 16:04 xitanggg

There's some limited information in the Amplify docs: https://aws-amplify.github.io/docs/cli-toolchain/quickstart#administrative-actions

For more detailed information, check out the cognito docs: https://docs.aws.amazon.com/cognito/index.html

The API reference might be useful: https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/Welcome.html

Let me know if that helps.

jhockett avatar Apr 13 '20 16:04 jhockett

Thanks a bunch for prompt response. The examples seem to be a great starting point. I know I came across it at some point but struggle to find it this time (no luck searching for Amplify Admin Queries on google). But this is exactly what I am looking for. Thanks again.

xitanggg avatar Apr 13 '20 18:04 xitanggg

@xitanggg Although it's too late but it may help others.

This section has very short description of predefined AdminQueries api end-pints

https://docs.amplify.aws/cli/auth/admin#admin-queries-api

When we create a amplify cli project and allow AdminQueries, it creates a lambda function inside amplify/backend/function/AdminQueriesXXXXXXX

This folder contains following files

src/ - index.js - app.js - cognitoActions.js` - package.json

app.js contains express based api endpoints e.g.

app.post('/addUserToGroup', async (req, res, next) => {
  if (!req.body.username || !req.body.groupname) {
    const err = new Error('username and groupname are required');
    err.statusCode = 400;
    return next(err);
  }

  try {
    const response = await addUserToGroup(req.body.username, req.body.groupname);
    res.status(200).json(response);
  } catch (err) {
    next(err);
  }
});

By default, you get following endpoints/sub-functions

  • addUserToGroup
  • removeUserFromGroup
  • confirmUserSignUp
  • disableUser
  • enableUser
  • getUser
  • listUsers
  • listGroups
  • listGroupsForUser
  • listUsersInGroup
  • signUserOut

You can add any endpoint and do any thing that AWS SDK (js) provides. e.g. you can add another endpoint to create a user as a post request. createUser function used in code below is imported from cognitoActions.js file (which you need to write and export in that file) or you can even write this logic right inside this api endpoint callback.

app.post('/createUser', async (req, res, next) => {
  try {
    const response = await createUser(req.body);
    res.status(200).json(response);
  } catch (err) {
    next(err);
  }
});

zohaib-enquizit avatar Nov 20 '20 12:11 zohaib-enquizit

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

github-actions[bot] avatar May 25 '21 13:05 github-actions[bot]