Commit 3085c95e authored by Yoon, Daeki's avatar Yoon, Daeki 😅
Browse files

delete user 기능 추가

parent 960b511c
import { NextFunction, Request, Response } from "express";
import { userDb } from "../db"; import { userDb } from "../db";
import { asyncWrap } from "../helpers/asyncWrap"; import { asyncWrap } from "../helpers/asyncWrap";
import { TypedRequestAuth } from "./auth.controller";
export const getUsers = asyncWrap(async (req, res) => { export const getUsers = asyncWrap(async (req, res) => {
const users = await userDb.getUsers(); const users = await userDb.getUsers();
return res.json(users); return res.json(users);
...@@ -10,5 +11,31 @@ export const createUser = asyncWrap(async (req, res) => { ...@@ -10,5 +11,31 @@ export const createUser = asyncWrap(async (req, res) => {
const user = req.body; const user = req.body;
console.log("user body", user); console.log("user body", user);
const newUser = await userDb.createUser(user); const newUser = await userDb.createUser(user);
return res.json(user); return res.json(newUser);
});
export const deleteUser = asyncWrap(async (req, res) => {
const { userId } = req.params;
console.log("user id:", userId);
const deletedUser = await userDb.deleteUserById(userId);
return res.json(deletedUser);
}); });
export const userById = async (
reqExp: Request,
res: Response,
next: NextFunction,
userId: string
) => {
try {
const req = reqExp as TypedRequestAuth<{ userId: string }>;
let user = await userDb.findUserById(userId);
if (!user) {
return res.status(404).send("사용자를 찾을 수 없습니다");
}
req.user = user;
next();
} catch (error: any) {
return res.status(500).send(error.message || "사용자 찾기 중 오류");
}
};
import bcrypt from "bcryptjs";
import { IUser, User } from "../models"; import { IUser, User } from "../models";
export const createUser = async (user: IUser) => { export const createUser = async (user: IUser) => {
const newUser = await User.create(user); // 비밀번호 암호화
const hash = await bcrypt.hash(user.password, 10);
const newUser = await User.create({ email: user.email, password: hash });
return newUser; return newUser;
}; };
export const deleteUserById = async (userId: string) => {
const deletedUser = await User.findByIdAndDelete(userId);
return deletedUser;
};
export const findUserById = async (id: string) => {
const user = await User.findById(id);
return user;
};
export const findUserByEmail = async ( export const findUserByEmail = async (
email: string, email: string,
includePassword: boolean = false includePassword: boolean = false
......
...@@ -8,4 +8,10 @@ router ...@@ -8,4 +8,10 @@ router
.get(authCtrl.requireLogin, userCtrl.getUsers) .get(authCtrl.requireLogin, userCtrl.getUsers)
.post(authCtrl.requireLogin, userCtrl.createUser); .post(authCtrl.requireLogin, userCtrl.createUser);
router
.route("/:userId")
.delete(authCtrl.requireLogin, authCtrl.authenticate, userCtrl.deleteUser);
router.param("userId", userCtrl.userById);
export default router; export default router;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment