Skip to content

Commit

Permalink
test 2
Browse files Browse the repository at this point in the history
  • Loading branch information
thebkht committed Oct 30, 2023
1 parent 4c68aac commit ecf2e75
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 54 deletions.
64 changes: 64 additions & 0 deletions app/api/auth/session/route.ts
Original file line number Diff line number Diff line change
@@ -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 })
}
}
62 changes: 8 additions & 54 deletions components/get-session-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit ecf2e75

Please sign in to comment.