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

계정삭제-로그아웃

parent 0ae2a2b5
......@@ -6,14 +6,11 @@ export const profile = async () => {
return data;
};
export const picture = async (formdata: FormData) => {
await axios.post(`${baseUrl}/profile`, formdata);
};
export const nickname = async (formdata: FormData) => {
export const profileUpload = async (formdata: FormData) => {
await axios.post(`${baseUrl}/profile`, formdata);
};
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";
import { asyncWrap } from "../helpers/asyncWrap";
import { Request } from "express";
import formidable from "formidable";
import { ObjectId } from "mongoose";
import fs from "fs";
import { TypedRequest } from "../types";
export interface TypedRequestAuth<T> extends Request {
auth: T;
......@@ -30,7 +29,7 @@ export const getProfile = 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 form = formidable({
......@@ -66,9 +65,11 @@ export const postPicture = 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 profile = await userDb.deleteUser(userId);
res.json(profile);
const finish = await userDb.deleteUser(userId);
if (finish?.deletedCount === 1) {
res.json(true);
}
});
import bcrypt from "bcryptjs";
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";
export const createUser = async (user: IUser) => {
// 비밀번호 암호화
const hash = await bcrypt.hash(user.password, 10);
const newAvatar = await Avatar.create({});
const newFileInfo = await FileInfo.create({});
// 사용자 역할 추가: 기본값은 "user"
let userRole = null;
if (user.role) {
......@@ -19,7 +19,7 @@ export const createUser = async (user: IUser) => {
password: hash,
role: userRole,
isNew: true,
avatar: newAvatar,
fileInfo: newFileInfo._id,
});
const retUser = await newUser.save();
return retUser;
......@@ -44,7 +44,7 @@ export const findUserByPostId = async (postId: 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; //이름 수정
};
......@@ -80,19 +80,19 @@ export const postPicture = async (
) => {
const profile = await User.findById(userId);
if (!(profile?.avatar === undefined)) {
if (!(profile?.fileInfo === undefined)) {
if (originalfilename === null) {
await Avatar.findByIdAndUpdate(profile.avatar._id, {
await FileInfo.findByIdAndUpdate(profile.fileInfo._id, {
nickname: nickname,
});
} else if (nickname === "") {
await Avatar.findByIdAndUpdate(profile.avatar._id, {
await FileInfo.findByIdAndUpdate(profile.fileInfo._id, {
originalfilename: originalfilename,
newfilename: newfilename,
picturepath: picturepath,
});
} else {
await Avatar.findByIdAndUpdate(profile.avatar._id, {
await FileInfo.findByIdAndUpdate(profile.fileInfo._id, {
originalfilename: originalfilename,
newfilename: newfilename,
picturepath: picturepath,
......@@ -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);
if (!(user?.avatar === undefined)) {
const ref = await Avatar.findById(user.avatar._id);
if (!(user?.fileInfo === undefined)) {
const ref = await FileInfo.findById(user.fileInfo._id);
if (!(ref?.newfilename === undefined)) {
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 { userCtrl, authCtrl } from "../controllers";
import { userCtrl, authCtrl, fileInfoCtrl } from "../controllers";
const router = express.Router();
router
.route("/")
.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);
......
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