diff --git a/src/pages/cli/auth/admin.mdx b/src/pages/cli/auth/admin.mdx index a71796da6b2..1603316ab90 100644 --- a/src/pages/cli/auth/admin.mdx +++ b/src/pages/cli/auth/admin.mdx @@ -263,4 +263,52 @@ class MyCustomInterceptor: URLRequestInterceptor { } ``` - \ No newline at end of file + + +## 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); + } +}); +``` \ No newline at end of file