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

duplicate key error clear

parent 94bd52bc
...@@ -35,8 +35,7 @@ export const login = asyncWrap(async (req, res) => { ...@@ -35,8 +35,7 @@ export const login = asyncWrap(async (req, res) => {
maxAge: cookieConfig.maxAge, // 이 기간 내에서만 유효 maxAge: cookieConfig.maxAge, // 이 기간 내에서만 유효
path: "/", //어떠한 경로에 관해서만 쓴다. 지금은 전부에 쓴다. path: "/", //어떠한 경로에 관해서만 쓴다. 지금은 전부에 쓴다.
httpOnly: envConfig.mode === "production", //false면 브라우저에서 쿠키를 조작, true면 조작할 수 없다. httpOnly: envConfig.mode === "production", //false면 브라우저에서 쿠키를 조작, true면 조작할 수 없다.
secure: envConfig.mode === "production", //true 면 https를 통해서만 쿠키 전달, false면 secure: envConfig.mode === "production", // true면 https를 통해서만 쿠키 전달, false면 불가능
}); });
// 5) 사용자 반환 // 5) 사용자 반환
res.json({ res.json({
...@@ -91,6 +90,7 @@ export const signup = asyncWrap(async (req, res) => { ...@@ -91,6 +90,7 @@ export const signup = asyncWrap(async (req, res) => {
const hash = await bcrypt.hash(password, 10); const hash = await bcrypt.hash(password, 10);
// 4) 새로운 사용자 만들기 // 4) 새로운 사용자 만들기
const newUser = await userDb.createUser({ const newUser = await userDb.createUser({
name,
email, email,
password, password,
}); });
......
import { NextFunction, Request, Response } from "express"; import { NextFunction, Request, Response } from "express";
import isLength from "validator/lib/isLength"; import isLength from "validator/lib/isLength";
import equals from "validator/lib/equals"; import equals from "validator/lib/equals";
import { requireLogin } from "./auth.controller";
import { asyncWrap } from "../helpers"; import { asyncWrap } from "../helpers";
import { postDb } from "../db"; import { postDb } from "../db";
export const posting = asyncWrap(async (req, res) => { export const posting = asyncWrap(async (req, res) => {
const { title, text, theme, city, username } = req.body; const { title, text, theme, city, username, date, counts } = req.body;
console.log("body", req.body); console.log("body", req.body);
// 0) 로그인 했는지 확인 requireLogin
// 1) title 빈 문자열인지 확인 // 1) title 빈 문자열인지 확인
if (!isLength(title ?? "", { min: 1 })) { if (!isLength(title ?? "", { min: 1 })) {
return res.status(422).send("제목을 한 글자 이상 입력해주세요"); return res.status(422).send("제목을 한 글자 이상 입력해주세요");
...@@ -29,27 +32,16 @@ export const posting = asyncWrap(async (req, res) => { ...@@ -29,27 +32,16 @@ export const posting = asyncWrap(async (req, res) => {
return res.status(422).send("도시를 선택해 주세요"); return res.status(422).send("도시를 선택해 주세요");
} }
// 5) username 확인 필요 없음 // 5) username ref: cookie.token._id -> collection users, "User"-> name
// 6)
const newPosting = await postDb.createPosting({ const newPosting = await postDb.createPosting({
title, title,
text, text,
theme, theme,
city, city,
username, username,
date,
counts,
}); });
return res.json(newPosting); return res.json(newPosting);
}); });
export const post = asyncWrap(async (req, res) => {
const { title, theme, city, username, date, counts } = req.body;
console.log("body", req.body);
// 1) DB postings에서 title, theme, city, username, date 불러오기
// 2) 불러온 데이터 + counts posts에 저장
// 3) DB posts에서 데이터 불러서 frontend로 보내기
});
...@@ -8,16 +8,8 @@ export const createPosting = async (posting: PostingType) => { ...@@ -8,16 +8,8 @@ export const createPosting = async (posting: PostingType) => {
theme: posting.theme, theme: posting.theme,
city: posting.city, city: posting.city,
username: posting.username, username: posting.username,
date: posting.date,
counts: 0,
}); });
return newPosting; return newPosting;
}; };
export const createPost = async (post: PostType) => {
const newPost = await Post.create({
title: post.title,
theme: post.theme,
city: post.city,
date: post.date,
counts: post.counts,
});
};
...@@ -4,7 +4,11 @@ import { IUser, User } from "../models"; ...@@ -4,7 +4,11 @@ import { IUser, User } from "../models";
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 newUser = await User.create({ email: user.email, password: hash }); const newUser = await User.create({
email: user.email,
password: hash,
name: user.name,
});
return newUser; return newUser;
}; };
......
...@@ -6,6 +6,8 @@ export interface PostingType { ...@@ -6,6 +6,8 @@ export interface PostingType {
theme: string; theme: string;
city: string; city: string;
username?: string; username?: string;
date?: string;
counts?: number;
} }
const postingSchema = new Schema<PostingType>({ const postingSchema = new Schema<PostingType>({
...@@ -25,6 +27,14 @@ const postingSchema = new Schema<PostingType>({ ...@@ -25,6 +27,14 @@ const postingSchema = new Schema<PostingType>({
}, },
username: { username: {
type: String, type: String,
// username: travelreport.users.findOne({id: cookie.token._id(Schema.Tpyes.ObjectId)}).name,
},
date: {
type: Date,
default: Date.now,
},
counts: {
type: Number,
}, },
}); });
......
...@@ -8,6 +8,5 @@ const router = express.Router(); ...@@ -8,6 +8,5 @@ const router = express.Router();
router.use("/users", userRouter); router.use("/users", userRouter);
router.use("/auth", authRouter); router.use("/auth", authRouter);
router.use("/posts", postRouter); router.use("/posts", postRouter);
//posting함수 -> mongodb에 posts json형식으로 저장
export default router; export default router;
import express from "express"; import express from "express";
import { postCtrl } from "../controllers"; import { postCtrl, authCtrl } from "../controllers";
const router = express.Router(); const router = express.Router();
router.route("/").post(postCtrl.posting); router.route("/").post(authCtrl.requireLogin, postCtrl.posting);
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