-
Notifications
You must be signed in to change notification settings - Fork 769
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { db } from "@/server/db"; | ||
import { sessions, space, users } from "@/server/db/schema"; | ||
import { eq } from "drizzle-orm"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
export async function GET(req: NextRequest) { | ||
|
||
const token = | ||
req.cookies.get("next-auth.session-token")?.value ?? | ||
req.cookies.get("__Secure-authjs.session-token")?.value ?? | ||
req.cookies.get("authjs.session-token")?.value ?? | ||
req.headers.get("Authorization")?.replace("Bearer ", ""); | ||
|
||
if (!token) { | ||
return new Response( | ||
JSON.stringify({ message: "Invalid Key, session not found." }), | ||
{ status: 404 }, | ||
); | ||
} | ||
|
||
if (process.env.RATELIMITER) { | ||
const { success } = await process.env.RATELIMITER.limit({ key: token }); | ||
|
||
if (!success) { | ||
return new Response(JSON.stringify({ message: "Rate limit exceeded" }), { | ||
status: 429, | ||
}); | ||
} | ||
} | ||
|
||
const sessionData = await db | ||
.select() | ||
.from(sessions) | ||
.where(eq(sessions.sessionToken, token!)); | ||
|
||
if (!sessionData || sessionData.length === 0) { | ||
return new Response( | ||
JSON.stringify({ message: "Invalid Key, session not found." }), | ||
{ status: 404 }, | ||
); | ||
} | ||
|
||
const userData = await db | ||
.select() | ||
.from(users) | ||
.where(eq(users.id, sessionData[0].userId)) | ||
.limit(1); | ||
|
||
if (!userData || userData.length === 0) { | ||
return NextResponse.json( | ||
{ message: "Invalid Key, session not found." }, | ||
{ status: 404 }, | ||
); | ||
} | ||
|
||
const user = userData[0] | ||
|
||
|
||
const spaces = await db | ||
.select() | ||
.from(space) | ||
.where(eq(space.user, user.id)) | ||
.all() | ||
|
||
return NextResponse.json({ | ||
message: "OK", | ||
data: spaces | ||
}, { status: 200 }) | ||
|
||
} |