From ecf2e75e6ab768ef7b2b8d7e427c62103ba2aff0 Mon Sep 17 00:00:00 2001 From: Bakhtiyor Ganijon Date: Tue, 31 Oct 2023 02:14:49 +0900 Subject: [PATCH] test 2 --- app/api/auth/session/route.ts | 64 ++++++++++++++++++++++++++++++++++ components/get-session-user.ts | 62 +++++--------------------------- 2 files changed, 72 insertions(+), 54 deletions(-) create mode 100644 app/api/auth/session/route.ts diff --git a/app/api/auth/session/route.ts b/app/api/auth/session/route.ts new file mode 100644 index 00000000..0e3cf3f9 --- /dev/null +++ b/app/api/auth/session/route.ts @@ -0,0 +1,64 @@ +import { config } from "@/app/auth"; +import postgres from "@/lib/postgres"; +import { getServerSession } from "next-auth"; +import { NextRequest, NextResponse } from "next/server"; + +export async function GET(req: NextRequest) { + try { + const session = await getServerSession(config) + if (!session) { + return NextResponse.json({ error: "Not authorized" }, { status: 401 }) + } + const { user } = session + + const result = await postgres.user.findFirst({ + where: { + image: user?.image, + }, + include: { + Followers: { + include: { + follower: { + include: { + Followers: true, + Followings: true, + }, + }, + }, + }, + Followings: { + include: { + following: { + include: { + Followers: true, + Followings: true, + }, + }, + }, + }, + bookmarks: { + include: { + post: { + include: { + author: true, + } + } + } + }, + posts: true, + notifications: true, + settings: true, + tagfollower: { + include: { + tag: true, + } + }, + } + }) + + return NextResponse.json({ user: JSON.parse(JSON.stringify(result)) }, { status: 200 }) + } catch (error) { + console.error(error) + return NextResponse.json({ error: "Something went wrong" }, { status: 500 }) + } +} \ No newline at end of file diff --git a/components/get-session-user.ts b/components/get-session-user.ts index 03608153..8d4bfb21 100644 --- a/components/get-session-user.ts +++ b/components/get-session-user.ts @@ -6,58 +6,12 @@ import { getServerSession } from "next-auth"; import { cache } from "react"; export async function getSessionUser() { - const session = await getServerSession(config); - if (!session) { - return null - } - try { - const { user } = session; - const result = await postgres.user.findFirst({ - where: { - image: user?.image, - }, - include: { - Followers: { - include: { - follower: { - include: { - Followers: true, - Followings: true, - }, - }, - }, - }, - Followings: { - include: { - following: { - include: { - Followers: true, - Followings: true, - }, - }, - }, - }, - bookmarks: { - include: { - post: { - include: { - author: true, - } - } - } - }, - posts: true, - notifications: true, - settings: true, - tagfollower: { - include: { - tag: true, - } - }, - } - }) - return JSON.parse(JSON.stringify(result)); - } catch (error) { - console.error(error); - } + const session = await fetch('/api/auth/session', { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + }, + }).then((res) => res.json()) + + return session.user } \ No newline at end of file