From 280fe2855e5fb4282f8be82a4556bfa9788305b5 Mon Sep 17 00:00:00 2001 From: Bakhtiyor Ganijon Date: Tue, 31 Oct 2023 02:33:19 +0900 Subject: [PATCH] Update fetch session user --- app/api/session/route.ts | 9 +++------ components/feed/get-feed.ts | 2 -- components/get-session-user.ts | 23 +++++++++++++++-------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/app/api/session/route.ts b/app/api/session/route.ts index 0e3cf3f9..6b330957 100644 --- a/app/api/session/route.ts +++ b/app/api/session/route.ts @@ -3,13 +3,10 @@ import postgres from "@/lib/postgres"; import { getServerSession } from "next-auth"; import { NextRequest, NextResponse } from "next/server"; -export async function GET(req: NextRequest) { +export async function POST(req: NextRequest) { try { - const session = await getServerSession(config) - if (!session) { - return NextResponse.json({ error: "Not authorized" }, { status: 401 }) - } - const { user } = session + const { user } = await req.json(); + const result = await postgres.user.findFirst({ where: { diff --git a/components/feed/get-feed.ts b/components/feed/get-feed.ts index 539d22b0..cd7cfdcf 100644 --- a/components/feed/get-feed.ts +++ b/components/feed/get-feed.ts @@ -1,6 +1,4 @@ 'use server' -import postgres from "@/lib/postgres"; -import { getSessionUser } from "../get-session-user"; import { getFeed } from "@/lib/prisma/feed"; export const fetchFeed = async ({ page = 0, tag }: { page?: number, tag?: string | undefined }) => { diff --git a/components/get-session-user.ts b/components/get-session-user.ts index 97f3e239..cffa4175 100644 --- a/components/get-session-user.ts +++ b/components/get-session-user.ts @@ -1,17 +1,24 @@ 'use server' -import { config } from "@/app/auth"; -import postgres from "@/lib/postgres"; -import { tr } from "date-fns/locale"; -import { getServerSession } from "next-auth"; -import { cache } from "react"; + +import { config } from "@/app/auth" +import { getServerSession } from "next-auth" export async function getSessionUser() { - const session = await fetch('/api/session', { - method: 'GET', + const sessionUser = await getServerSession(config) + if (!sessionUser) { + return null + } + const { user } = sessionUser + const session = await fetch(`${process.env.DOMAIN}/api/session`, { + method: 'POST', headers: { 'Content-Type': 'application/json', }, + body: JSON.stringify({ user }) }).then((res) => res.json()) - + console.log(session) + if (session.error) { + return null + } return session.user } \ No newline at end of file