Skip to content

Commit

Permalink
feat: add delete user api
Browse files Browse the repository at this point in the history
  • Loading branch information
Hellol77 committed Feb 19, 2024
1 parent 4ffe122 commit 5c3d1d9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions backend/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ functions:
method: GET
path: /user/profile/{proxy+}
cors: true
- httpApi:
method: POST
path: /user/delete
cors: true
likePost:
handler: src/index.handler
events:
Expand Down
40 changes: 40 additions & 0 deletions backend/src/controllers/user/deleteUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { User } from "../../models/user";
import { getAccessTokenToheader } from "./../../utils/getAccessTokenToHeader";
import { Request, Response } from "express";
import { getUserObjectId } from "../../utils/getUserObjectId";
import axios from "axios";
import { Post } from "../../models/post";
import { Comment } from "../../models/comment";
import { ReportedComment } from "../../models/repostComment";
import { ReportedPost } from "../../models/reportPost";

export const deleteUser = async (req: Request, res: Response) => {
const accessToken = getAccessTokenToheader(req);
if (!accessToken) {
return res.status(401).send("accessToken이 없습니다.");
}
const _id = await getUserObjectId(req, res, accessToken);
if (!_id) {
return res.status(401).send("Unauthorized. Fail to get user object id");
}
try {
const deleteUserData = await axios.post(
"https://kapi.kakao.com/v1/user/unlink",
{},
{
headers: {
Authorization: "Bearer " + accessToken,
"Content-type": "application/x-www-form-urlencoded",
},
}
);
await User.deleteOne({ userId: deleteUserData.data.id });
await Post.deleteMany({ user: _id });
await Comment.deleteMany({ user: _id });
await ReportedComment.deleteMany({ reportUser: _id });
await ReportedPost.deleteMany({ reportUser: _id });
return res.status(200).send("Success Delete User");
} catch (err) {
return res.status(400).send("Fail to delete user");
}
};

0 comments on commit 5c3d1d9

Please sign in to comment.