7.6 익명/비밀댓글/비댓 해시 처리 o
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import prisma from "@/lib/prisma";
|
||||
import { z } from "zod";
|
||||
import { hashPassword } from "@/lib/password";
|
||||
|
||||
const createCommentSchema = z.object({
|
||||
postId: z.string().min(1),
|
||||
@@ -8,6 +9,7 @@ const createCommentSchema = z.object({
|
||||
content: z.string().min(1),
|
||||
isAnonymous: z.boolean().optional(),
|
||||
isSecret: z.boolean().optional(),
|
||||
secretPassword: z.string().min(1).optional(), // 비회원 비밀번호(옵션)
|
||||
});
|
||||
|
||||
export async function POST(req: Request) {
|
||||
@@ -16,7 +18,8 @@ export async function POST(req: Request) {
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
|
||||
}
|
||||
const { postId, authorId, content, isAnonymous, isSecret } = parsed.data;
|
||||
const { postId, authorId, content, isAnonymous, isSecret, secretPassword } = parsed.data;
|
||||
const secretPasswordHash = secretPassword ? hashPassword(secretPassword) : null;
|
||||
const comment = await prisma.comment.create({
|
||||
data: {
|
||||
postId,
|
||||
@@ -24,6 +27,7 @@ export async function POST(req: Request) {
|
||||
content,
|
||||
isAnonymous: !!isAnonymous,
|
||||
isSecret: !!isSecret,
|
||||
secretPasswordHash,
|
||||
},
|
||||
});
|
||||
return NextResponse.json({ comment }, { status: 201 });
|
||||
|
||||
@@ -11,10 +11,19 @@ export async function GET(_: Request, context: { params: Promise<{ id: string }>
|
||||
content: true,
|
||||
isAnonymous: true,
|
||||
isSecret: true,
|
||||
secretPasswordHash: true,
|
||||
createdAt: true,
|
||||
},
|
||||
});
|
||||
return NextResponse.json({ comments });
|
||||
const presented = comments.map((c) => ({
|
||||
id: c.id,
|
||||
content: c.isSecret ? "비밀댓글입니다." : c.content,
|
||||
isAnonymous: c.isAnonymous,
|
||||
isSecret: c.isSecret,
|
||||
anonId: c.isAnonymous ? c.id.slice(-6) : undefined,
|
||||
createdAt: c.createdAt,
|
||||
}));
|
||||
return NextResponse.json({ comments: presented });
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user