Skip to content

feat(CLI): How to add non-default admin actions #5237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion src/pages/cli/auth/admin.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,52 @@ class MyCustomInterceptor: URLRequestInterceptor {
}
```
</Block>
</BlockSwitcher>
</BlockSwitcher>

## Adding Admin Actions

To add additional admin actions that are not included by default but are enabled by Amazon Cognito, you will need to update the Lambda function code that is generated for you. The change will include adding a route handler for the action and creating a route for it. You will then associate the route handler to the route within the [Express](https://expressjs.com/) app.

Below is an example of how to add an admin action that will allow you to [update a user's attributes](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminUpdateUserAttributes.html).
```js
async function updateUserAttributes(username, attributes) {

const params = {
Username: username,
UserAttributes: attributes,
UserPoolId: 'STRING_VALUE',
};

console.log(`Attempting to update ${username} attributes`);

try {
await cognitoIdentityServiceProvider.adminUpdateUserAttributes(params).promise();
console.log(`Success updating ${username} attributes`);
return {
message: `Success updating ${username} attributes`,
};
} catch (err) {
console.log(err);
throw err;
}
}
```
Once the route handler is defined, you will then add a route with the correct HTTP method to the Express app and associate the route handler to the route. Be sure to make the route unique.

Below is an example of how you can add a `POST` route named `/updateUserAttributes` and associate the above route handler to it.
```js
app.post('/updateUserAttributes', async (req, res, next) => {
if (!req.body.username || !req.body.attributes) {
const err = new Error('username and attributes are required');
err.statusCode = 400;
return next(err);
}

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