Commit 3da19360 authored by Lee Soobeom's avatar Lee Soobeom
Browse files

Merge remote-tracking branch 'origin/MK16' into develop

parents e9ea9d71 48441161
...@@ -13,3 +13,7 @@ export const picture = async (formdata: FormData) => { ...@@ -13,3 +13,7 @@ export const picture = async (formdata: FormData) => {
export const nickname = async (formdata: FormData) => { export const nickname = async (formdata: FormData) => {
await axios.post(`${baseUrl}/profile`, formdata); await axios.post(`${baseUrl}/profile`, formdata);
}; };
export const deleteUser = async () => {
await axios.post(`${baseUrl}/profile/delete`);
};
import React, { useEffect, useState } from "react"; import React, { useEffect, useState } from "react";
import { Profile } from "../types"; import { Profile } from "../types";
import { profileApi } from "../apis"; import { profileApi } from "../apis";
import { useAuth } from "../auth/auth.context";
export default function Profile() { export default function Profile() {
// 로컬 저장소에는 로그인 여부만 저장 // 로컬 저장소에는 로그인 여부만 저장
...@@ -9,6 +10,7 @@ export default function Profile() { ...@@ -9,6 +10,7 @@ export default function Profile() {
const [file, setFile] = useState<File>(); const [file, setFile] = useState<File>();
const [imageSrc, setImageSrc] = useState(""); const [imageSrc, setImageSrc] = useState("");
const [nickname, setNickname] = useState(""); const [nickname, setNickname] = useState("");
const { logout } = useAuth();
const handleProfile = async () => { const handleProfile = async () => {
const user: Profile = await profileApi.profile(); const user: Profile = await profileApi.profile();
...@@ -57,6 +59,10 @@ export default function Profile() { ...@@ -57,6 +59,10 @@ export default function Profile() {
await profileApi.picture(formdata); await profileApi.picture(formdata);
}; };
const deleteClick = async () => {
await profileApi.deleteUser().then(() => logout());
};
useEffect(() => { useEffect(() => {
userChange(); userChange();
}, []); }, []);
...@@ -119,6 +125,9 @@ export default function Profile() { ...@@ -119,6 +125,9 @@ export default function Profile() {
<button onClick={handleClick} className=" border-2 "> <button onClick={handleClick} className=" border-2 ">
저장하기 저장하기
</button> </button>
<button onClick={deleteClick} className="border-2">
계정 삭제
</button>
</div> </div>
</form> </form>
</div> </div>
......
...@@ -74,3 +74,11 @@ export const postPicture = asyncWrap(async (reqExp, res) => { ...@@ -74,3 +74,11 @@ export const postPicture = asyncWrap(async (reqExp, res) => {
}); });
res.json(); res.json();
}); });
export const deleteUser = asyncWrap(async (reqExp, res) => {
const req = reqExp as TypedRequestAuth<{ userId: ObjectId }>; // 앞에서는 토큰으로써 사용하기 때문에 JwtPayload 를 사용하고 여기서는 verify 에서 토큰을 디코딩했기에 ObjectId 타입의 string으로 바뀌게 된다.
const { userId } = req.auth;
const profile = await userDb.deleteUser(userId);
res.json(profile);
});
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, Avatar } from "../models";
import fs from "fs";
export const createUser = async (user: IUser) => { export const createUser = async (user: IUser) => {
// 비밀번호 암호화 // 비밀번호 암호화
...@@ -87,3 +88,15 @@ export const postPicture = async ( ...@@ -87,3 +88,15 @@ export const postPicture = async (
}); });
} }
}; };
export const deleteUser = async (userId: ObjectId) => {
const user = await User.findById(userId);
if (!(user?.avatar === undefined)) {
const ref = await Avatar.findById(user.avatar._id);
fs.unlink("../travel/uploads/" + ref?.newfilename, (err) => {
console.log(err);
});
await Avatar.deleteOne({ _id: user.avatar._id });
await User.deleteOne({ _id: userId });
}
};
...@@ -8,4 +8,6 @@ router ...@@ -8,4 +8,6 @@ router
.get(authCtrl.requireLogin, userCtrl.getProfile) .get(authCtrl.requireLogin, userCtrl.getProfile)
.post(authCtrl.requireLogin, userCtrl.postPicture); //중간에 req쪽에 fields와 files 넣는 미들웨어 추가 .post(authCtrl.requireLogin, userCtrl.postPicture); //중간에 req쪽에 fields와 files 넣는 미들웨어 추가
router.route("/delete").post(authCtrl.requireLogin, userCtrl.deleteUser);
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