Commit 0be923a8 authored by Kim, MinGyu's avatar Kim, MinGyu
Browse files

계정삭제-로그아웃

parent 0ae2a2b5
...@@ -6,14 +6,11 @@ export const profile = async () => { ...@@ -6,14 +6,11 @@ export const profile = async () => {
return data; return data;
}; };
export const picture = async (formdata: FormData) => { export const profileUpload = async (formdata: FormData) => {
await axios.post(`${baseUrl}/profile`, formdata);
};
export const nickname = async (formdata: FormData) => {
await axios.post(`${baseUrl}/profile`, formdata); await axios.post(`${baseUrl}/profile`, formdata);
}; };
export const deleteUser = async () => { export const deleteUser = async () => {
await axios.post(`${baseUrl}/profile/delete`); const success = await axios.post(`${baseUrl}/profile/delete`);
return success;
}; };
...@@ -2,8 +2,7 @@ import { userDb } from "../db"; ...@@ -2,8 +2,7 @@ import { userDb } from "../db";
import { asyncWrap } from "../helpers/asyncWrap"; import { asyncWrap } from "../helpers/asyncWrap";
import { Request } from "express"; import { Request } from "express";
import formidable from "formidable"; import formidable from "formidable";
import { ObjectId } from "mongoose"; import { TypedRequest } from "../types";
import fs from "fs";
export interface TypedRequestAuth<T> extends Request { export interface TypedRequestAuth<T> extends Request {
auth: T; auth: T;
...@@ -30,7 +29,7 @@ export const getProfile = asyncWrap(async (reqExp, res) => { ...@@ -30,7 +29,7 @@ export const getProfile = asyncWrap(async (reqExp, res) => {
}); });
export const postPicture = asyncWrap(async (reqExp, res) => { export const postPicture = asyncWrap(async (reqExp, res) => {
const req = reqExp as TypedRequestAuth<{ userId: ObjectId }>; const req = reqExp as TypedRequest;
const { userId } = req.auth; const { userId } = req.auth;
const form = formidable({ const form = formidable({
...@@ -66,9 +65,11 @@ export const postPicture = asyncWrap(async (reqExp, res) => { ...@@ -66,9 +65,11 @@ export const postPicture = asyncWrap(async (reqExp, res) => {
}); });
export const deleteUser = asyncWrap(async (reqExp, res) => { export const deleteUser = asyncWrap(async (reqExp, res) => {
const req = reqExp as TypedRequestAuth<{ userId: ObjectId }>; // 앞에서는 토큰으로써 사용하기 때문에 JwtPayload 를 사용하고 여기서는 verify 에서 토큰을 디코딩했기에 ObjectId 타입의 string으로 바뀌게 된다. const req = reqExp as TypedRequestAuth<{ userId: string }>; // 앞에서는 토큰으로써 사용하기 때문에 JwtPayload 를 사용하고 여기서는 verify 에서 토큰을 디코딩했기에 ObjectId 타입의 string으로 바뀌게 된다.
const { userId } = req.auth; const { userId } = req.auth;
const profile = await userDb.deleteUser(userId); const finish = await userDb.deleteUser(userId);
res.json(profile); if (finish?.deletedCount === 1) {
res.json(true);
}
}); });
import bcrypt from "bcryptjs"; import bcrypt from "bcryptjs";
import { ObjectId } from "mongoose"; import { ObjectId } from "mongoose";
import { IUser, Role, Post, User, Avatar } from "../models"; import { IUser, Role, Post, User, FileInfo } from "../models";
import fs from "fs/promises"; import fs from "fs/promises";
export const createUser = async (user: IUser) => { export const createUser = async (user: IUser) => {
// 비밀번호 암호화 // 비밀번호 암호화
const hash = await bcrypt.hash(user.password, 10); const hash = await bcrypt.hash(user.password, 10);
const newAvatar = await Avatar.create({}); const newFileInfo = await FileInfo.create({});
// 사용자 역할 추가: 기본값은 "user" // 사용자 역할 추가: 기본값은 "user"
let userRole = null; let userRole = null;
if (user.role) { if (user.role) {
...@@ -19,7 +19,7 @@ export const createUser = async (user: IUser) => { ...@@ -19,7 +19,7 @@ export const createUser = async (user: IUser) => {
password: hash, password: hash,
role: userRole, role: userRole,
isNew: true, isNew: true,
avatar: newAvatar, fileInfo: newFileInfo._id,
}); });
const retUser = await newUser.save(); const retUser = await newUser.save();
return retUser; return retUser;
...@@ -44,7 +44,7 @@ export const findUserByPostId = async (postId: string) => { ...@@ -44,7 +44,7 @@ export const findUserByPostId = async (postId: string) => {
}; };
export const getProfile = async (userId: string) => { export const getProfile = async (userId: string) => {
const profile = await User.findById(userId).populate("avatar"); const profile = await User.findById(userId).populate("fileInfo");
return profile; //이름 수정 return profile; //이름 수정
}; };
...@@ -80,19 +80,19 @@ export const postPicture = async ( ...@@ -80,19 +80,19 @@ export const postPicture = async (
) => { ) => {
const profile = await User.findById(userId); const profile = await User.findById(userId);
if (!(profile?.avatar === undefined)) { if (!(profile?.fileInfo === undefined)) {
if (originalfilename === null) { if (originalfilename === null) {
await Avatar.findByIdAndUpdate(profile.avatar._id, { await FileInfo.findByIdAndUpdate(profile.fileInfo._id, {
nickname: nickname, nickname: nickname,
}); });
} else if (nickname === "") { } else if (nickname === "") {
await Avatar.findByIdAndUpdate(profile.avatar._id, { await FileInfo.findByIdAndUpdate(profile.fileInfo._id, {
originalfilename: originalfilename, originalfilename: originalfilename,
newfilename: newfilename, newfilename: newfilename,
picturepath: picturepath, picturepath: picturepath,
}); });
} else { } else {
await Avatar.findByIdAndUpdate(profile.avatar._id, { await FileInfo.findByIdAndUpdate(profile.fileInfo._id, {
originalfilename: originalfilename, originalfilename: originalfilename,
newfilename: newfilename, newfilename: newfilename,
picturepath: picturepath, picturepath: picturepath,
...@@ -102,12 +102,15 @@ export const postPicture = async ( ...@@ -102,12 +102,15 @@ export const postPicture = async (
} }
}; };
export const deleteUser = async (userId: ObjectId) => { export const deleteUser = async (userId: string) => {
const user = await User.findById(userId); const user = await User.findById(userId);
if (!(user?.avatar === undefined)) { if (!(user?.fileInfo === undefined)) {
const ref = await Avatar.findById(user.avatar._id); const ref = await FileInfo.findById(user.fileInfo._id);
if (!(ref?.newfilename === undefined)) {
await fs.unlink("../travel/uploads/" + ref?.newfilename); await fs.unlink("../travel/uploads/" + ref?.newfilename);
await Avatar.deleteOne({ _id: user.avatar._id }); }
await User.deleteOne({ _id: userId }); await FileInfo.deleteOne({ _id: user.fileInfo._id });
const finish = await User.deleteOne({ _id: userId });
return finish;
} }
}; };
import express from "express"; import express from "express";
import { userCtrl, authCtrl } from "../controllers"; import { userCtrl, authCtrl, fileInfoCtrl } from "../controllers";
const router = express.Router(); const router = express.Router();
router router
.route("/") .route("/")
.get(authCtrl.requireLogin, userCtrl.getProfile) .get(authCtrl.requireLogin, userCtrl.getProfile)
.post(authCtrl.requireLogin, userCtrl.postPicture); //중간에 req쪽에 fields와 files 넣는 미들웨어 추가 .post(authCtrl.requireLogin, fileInfoCtrl.uploadFiles, userCtrl.postPicture); //중간에 req쪽에 fields와 files 넣는 미들웨어 추가
router.route("/delete").post(authCtrl.requireLogin, userCtrl.deleteUser); router.route("/delete").post(authCtrl.requireLogin, userCtrl.deleteUser);
......
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