I am playing with this tutorial, https://aws.amazon.com/ko/blogs/mobile/new-real-time-multi-group-app-with-aws-amplify-graphql-build-a-twitter-community-clone/
I wanted to add the delete function for cognito group. But I could not.
This is What I did.
1.
amplify/backend/api/twittercommunity/schema.graphql
type Tweet @model @auth(rules: [
{ allow: owner, operations: [create, delete] },
{ allow: groups, groupsField: "community", operations: [read] }
]) {
content: String!
community: String!
}
I add "delete"
operations: [read, delete]
- amplify/backend/function/AdminQueries/src app.js file:
added this
const {
addUserToGroup,
removeUserFromGroup,
addAndJoinGroup,
listGroups,
deleteGroup,
listGroupsForUser,
listUsersInGroup,
} = require('./cognitoActions');
app.get('/deleteGroup', async (req, res, next) => {
if (!req.body.groupname) {
const err = new Error('Groupname is required');
err.statusCode = 400;
return next(err);
}
try {
const username = req.apiGateway.event.requestContext.authorizer.claims.username
const response = await deleteGroup(username, req.body.groupname);
res.status(200).json(response);
} catch (err) {
next(err);
}
});
and in the cogintoActions.js
async function deleteGroup(username, groupname) {
const params = {
GroupName: groupname,
UserPoolId: userPoolId
};
console.log('delete group', params)
console.log(Attempting to remove ${groupname} from ${userPoolId}
);
cognitoIdentityServiceProvider.deleteGroup(params, function (err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
});
module.exports = {
addUserToGroup,
removeUserFromGroup,
addAndJoinGroup,
listGroups,
listGroupsForUser,
listUsersInGroup,
deleteGroup
};
and the amplify/backend/function/AdminQueriesXYZ/AdminQueriesXYZ-cloudformation-template.json
at the “PolicyDocument” section, added it.
"cognito-idp:DeleteGroup",
and the src/utils.js
export async function deleteGroup(groupName) {
console.log('deleteGroup: ', groupName)
await API.post(
API_NAME,
'/deleteGroup',
{
body: {
groupname: groupName
},
headers: {
'Content-Type': 'application/json',
Authorization: ${(await Auth.currentSession()).getAccessToken().getJwtToken()}
}
});
await invalidateRefreshToken();
}
and the delete button in the GroupPanels.js
Only group-creator can delete the group, and should enter right name of the groupname to delete.
async function handleDeleteGroupClick() {
await deleteGroup(window.prompt('Group Name?'))
refreshGroups()
}
<Button
size="small"
variation="link"
onClick={() => handleDeleteGroupClick()}
isDisabled={user.attributes.sub !== owner}>
delete
</Button>
However, It gave an 404 error. I do not know how to fix it any more. please help me out.