diff options
| author | Dhravya <[email protected]> | 2024-02-27 11:35:51 -0700 |
|---|---|---|
| committer | Dhravya <[email protected]> | 2024-02-27 11:35:51 -0700 |
| commit | 3f056ccbe834d0edd7c9d964d46d055601e680db (patch) | |
| tree | 60ff46d7e5e2d25d96c8e4f6a961fe912e584f9b /apps/web/src/app/api | |
| parent | set up cf ai worker (diff) | |
| download | supermemory-3f056ccbe834d0edd7c9d964d46d055601e680db.tar.xz supermemory-3f056ccbe834d0edd7c9d964d46d055601e680db.zip | |
set up cf ai worker
Diffstat (limited to 'apps/web/src/app/api')
| -rw-r--r-- | apps/web/src/app/api/store/route.ts | 51 |
1 files changed, 38 insertions, 13 deletions
diff --git a/apps/web/src/app/api/store/route.ts b/apps/web/src/app/api/store/route.ts index 4f9a8968..c80da0dc 100644 --- a/apps/web/src/app/api/store/route.ts +++ b/apps/web/src/app/api/store/route.ts @@ -2,24 +2,49 @@ import { db } from "@/server/db"; import { eq } from "drizzle-orm"; import { sessions, users } from "@/server/db/schema"; import { type NextRequest, NextResponse } from "next/server"; +import { env } from "@/env"; export const runtime = "edge"; -export async function GET(req: NextRequest) { - try { - 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 ", ""); +export async function POST(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 ", ""); - console.log(token ? token : 'token not found lol') - console.log(process.env.DATABASE) + console.log(token ? token : 'token not found lol') + console.log(process.env.DATABASE) - const session = await db.select().from(sessions).where(eq(sessions.sessionToken, token!)) - .leftJoin(users, eq(sessions.userId, users.id)).limit(1) + const session = await db.select().from(sessions).where(eq(sessions.sessionToken, token!)) + .leftJoin(users, eq(sessions.userId, users.id)).limit(1) - if (!session || session.length === 0) { - return NextResponse.json({ message: "Invalid Key, session not found." }, { status: 404 }); - } - return NextResponse.json({ message: "OK", data: session }, { status: 200 }); - } catch (error) { - return NextResponse.json({ message: "Error", error }, { status: 500 }); + if (!session || session.length === 0) { + return NextResponse.json({ message: "Invalid Key, session not found." }, { status: 404 }); } + + if (!session[0].user) { + return NextResponse.json({ message: "Invalid Key, session not found." }, { status: 404 }); + } + + const data = await req.json() as { + pageContent: string, + title?: string, + description?: string, + url: string, + }; + + + const resp = await fetch("https://cf-ai-backend.dhravya.workers.dev/add", { + method: "POST", + headers: { + "X-Custom-Auth-Key": env.BACKEND_SECURITY_KEY, + }, + body: JSON.stringify({ ...data, user: session[0].user.email }), + }); + + const _ = await resp.json(); + + if (resp.status !== 200) { + return NextResponse.json({ message: "Error", error: "Error in CF function" }, { status: 500 }); + } + + return NextResponse.json({ message: "OK", data: "Success" }, { status: 200 }); + }
\ No newline at end of file |