diff options
| author | Dhravya <[email protected]> | 2024-05-25 18:41:26 -0500 |
|---|---|---|
| committer | Dhravya <[email protected]> | 2024-05-25 18:41:26 -0500 |
| commit | 075f45986fd4d198292226e64afb71b3515576b4 (patch) | |
| tree | 5c728356cd0310f1c1c012fd6618c72a836c314b /apps/web/app/api | |
| parent | added social material (diff) | |
| download | supermemory-075f45986fd4d198292226e64afb71b3515576b4.tar.xz supermemory-075f45986fd4d198292226e64afb71b3515576b4.zip | |
refactored UI, with shared components and UI, better rules and million lint
Diffstat (limited to 'apps/web/app/api')
| -rw-r--r-- | apps/web/app/api/[...nextauth]/route.ts | 2 | ||||
| -rw-r--r-- | apps/web/app/api/ensureAuth.ts | 33 | ||||
| -rw-r--r-- | apps/web/app/api/hello/route.ts | 22 | ||||
| -rw-r--r-- | apps/web/app/api/upload_image/route.ts | 56 |
4 files changed, 113 insertions, 0 deletions
diff --git a/apps/web/app/api/[...nextauth]/route.ts b/apps/web/app/api/[...nextauth]/route.ts new file mode 100644 index 00000000..50807ab1 --- /dev/null +++ b/apps/web/app/api/[...nextauth]/route.ts @@ -0,0 +1,2 @@ +export { GET, POST } from "../../helpers/server/auth"; +export const runtime = "edge"; diff --git a/apps/web/app/api/ensureAuth.ts b/apps/web/app/api/ensureAuth.ts new file mode 100644 index 00000000..a1401a07 --- /dev/null +++ b/apps/web/app/api/ensureAuth.ts @@ -0,0 +1,33 @@ +import { NextRequest } from "next/server"; +import { db } from "../helpers/server/db"; +import { sessions, users } from "../helpers/server/db/schema"; +import { eq } from "drizzle-orm"; + +export async function ensureAuth(req: NextRequest) { + // A helper function to protect routes + + 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 undefined; + } + + const sessionData = await db + .select() + .from(sessions) + .innerJoin(users, eq(users.id, sessions.userId)) + .where(eq(sessions.sessionToken, token!)); + + if (!sessionData || sessionData.length < 0) { + return undefined; + } + + return { + user: sessionData[0]!.user, + session: sessionData[0]!, + }; +} diff --git a/apps/web/app/api/hello/route.ts b/apps/web/app/api/hello/route.ts new file mode 100644 index 00000000..363d0704 --- /dev/null +++ b/apps/web/app/api/hello/route.ts @@ -0,0 +1,22 @@ +import type { NextRequest } from "next/server"; +import { getRequestContext } from "@cloudflare/next-on-pages"; + +export const runtime = "edge"; + +export async function GET(request: NextRequest) { + let responseText = "Hello World"; + + // In the edge runtime you can use Bindings that are available in your application + // (for more details see: + // - https://developers.cloudflare.com/pages/framework-guides/deploy-a-nextjs-site/#use-bindings-in-your-nextjs-application + // - https://developers.cloudflare.com/pages/functions/bindings/ + // ) + // + // KV Example: + // const myKv = getRequestContext().env.MY_KV_NAMESPACE + // await myKv.put('suffix', ' from a KV store!') + // const suffix = await myKv.get('suffix') + // responseText += suffix + + return new Response(responseText); +} diff --git a/apps/web/app/api/upload_image/route.ts b/apps/web/app/api/upload_image/route.ts new file mode 100644 index 00000000..0d93c5b0 --- /dev/null +++ b/apps/web/app/api/upload_image/route.ts @@ -0,0 +1,56 @@ +import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3"; +import { getSignedUrl } from "@aws-sdk/s3-request-presigner"; + +import type { NextRequest } from "next/server"; +import { ensureAuth } from "../ensureAuth"; + +export const runtime = "edge"; + +export async function PUT(request: NextRequest) { + const d = await ensureAuth(request); + + if (!d) { + return new Response("Unauthorized", { status: 401 }); + } + + const reqUrl = new URL(request.url); + const filename = reqUrl.searchParams.get("filename"); + + if (!filename) { + return new Response("Missing filename", { status: 400 }); + } + + if ( + !process.env.R2_ENDPOINT || + !process.env.R2_ACCESS_ID || + !process.env.R2_SECRET_KEY || + !process.env.R2_BUCKET_NAME + ) { + return new Response( + "Missing one or more R2 env variables: R2_ENDPOINT, R2_ACCESS_ID, R2_SECRET_KEY, R2_BUCKET_NAME. To get them, go to the R2 console, create and paste keys in a `.dev.vars` file in the root of this project.", + { status: 500 }, + ); + } + + const s3 = new S3Client({ + region: "auto", + endpoint: process.env.R2_ENDPOINT, + credentials: { + accessKeyId: process.env.R2_ACCESS_ID, + secretAccessKey: process.env.R2_SECRET_KEY, + }, + }); + + const url = await getSignedUrl( + s3, + new PutObjectCommand({ Bucket: process.env.R2_BUCKET_NAME, Key: filename }), + { expiresIn: 3600 }, + ); + + return new Response(JSON.stringify({ url }), { + status: 200, + headers: { + "Content-Type": "application/json", + }, + }); +} |