Skip to content

Commit

Permalink
fix session bug
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghao1993 committed Sep 9, 2024
1 parent 207c7a5 commit 2ca7a0a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
11 changes: 7 additions & 4 deletions app/api/blog/detail/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import prisma from "@/lib/pg";
import { getServerSession } from "next-auth";
import { authOptions } from "@/lib/auth_options";
import { z } from "zod";
import { NextRequest, NextResponse } from "next/server";
import { getToken } from "next-auth/jwt";

export const dynamic = "force-dynamic";

Expand All @@ -11,7 +13,7 @@ const postDetailSchema = z.object({
// 其他字段的验证规则,如果有的话
});

export async function GET(request: Request) {
export async function GET(request: NextRequest) {
const key = new URL(request.url).searchParams.get("key");
if (!key) {
return responseHandler(
Expand Down Expand Up @@ -45,7 +47,7 @@ export async function GET(request: Request) {
}
}

export async function POST(req: Request, res: Response) {
export async function POST(req: NextRequest, res: NextResponse) {
const rawBody = await req.json();
const validationResult = postDetailSchema.safeParse(rawBody);
if (!validationResult.success) {
Expand All @@ -57,14 +59,15 @@ export async function POST(req: Request, res: Response) {
);
}
const body = validationResult.data;
const session = await getServerSession(authOptions);
const session = await getToken({ req, secret: process.env.SECRET_KEY });
const post = await prisma.post.findUnique({
where: { blog_key: body.blog_key },
});
console.log("session", session);
if (post && session) {
try {
const user = await prisma.user.findUnique({
where: { email: session.user.email as string },
where: { email: session.email as string },
});
if (!user) {
return responseHandler(
Expand Down
1 change: 0 additions & 1 deletion lib/auth_options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import prisma from "./pg";

export const authOptions: AuthOptions = {
secret: process.env.SECRET_KEY,
// adapter: PrismaAdapter(MongoPrisma as PrismaClient) as Adapter,
debug: true,
providers: [
GitHubProvider({
Expand Down

0 comments on commit 2ca7a0a

Please sign in to comment.