diff options
| author | MohamedBassem <me@mbassem.com> | 2024-02-06 01:15:14 +0000 |
|---|---|---|
| committer | MohamedBassem <me@mbassem.com> | 2024-02-06 01:15:14 +0000 |
| commit | d10b76b54745eb6f4972339caa4219cb1f50ae52 (patch) | |
| tree | 22cf575aeb173c8b100e8adc71620efedec2b3fc /app | |
| parent | 81531a46c368293b736bb3b12c413cfbe6e00ec4 (diff) | |
| download | karakeep-d10b76b54745eb6f4972339caa4219cb1f50ae52.tar.zst | |
[API] Add the POST /api/v1/links api
Diffstat (limited to 'app')
| -rw-r--r-- | app/api/auth/[...nextauth]/route.tsx | 22 | ||||
| -rw-r--r-- | app/api/v1/links/route.ts | 28 |
2 files changed, 30 insertions, 20 deletions
diff --git a/app/api/auth/[...nextauth]/route.tsx b/app/api/auth/[...nextauth]/route.tsx index b9decb30..bfcda516 100644 --- a/app/api/auth/[...nextauth]/route.tsx +++ b/app/api/auth/[...nextauth]/route.tsx @@ -1,21 +1,3 @@ -import NextAuth from "next-auth" -import { PrismaAdapter } from "@next-auth/prisma-adapter" -import AuthentikProvider from "next-auth/providers/authentik"; -import { PrismaClient } from "@prisma/client" -import serverConfig from "@/lib/config"; +import { authHandler } from "@/lib/auth"; -const prisma = new PrismaClient() - -let providers = []; - -if (serverConfig.auth.authentik) { - providers.push(AuthentikProvider(serverConfig.auth.authentik)); -} - -const handler = NextAuth({ - // Configure one or more authentication providers - adapter: PrismaAdapter(prisma), - providers: providers, -}); - -export { handler as GET, handler as POST } +export { authHandler as GET, authHandler as POST } diff --git a/app/api/v1/links/route.ts b/app/api/v1/links/route.ts new file mode 100644 index 00000000..9103830b --- /dev/null +++ b/app/api/v1/links/route.ts @@ -0,0 +1,28 @@ +import { authOptions } from "@/lib/auth"; +import prisma from "@/lib/prisma"; +import { getServerSession } from "next-auth"; +import { NextRequest, NextResponse } from "next/server"; + +interface NewLinkRequest { + url: string, +} + +export async function POST(request: NextRequest) { + // TODO: We probably should be using an API key here instead of the session; + const session = await getServerSession(authOptions); + if (!session) { + return new Response(null, { status: 401 }); + } + + // TODO: We need proper type assertion here + const body: NewLinkRequest = await request.json(); + + const link = await prisma.bookmarkedLink.create({ + data: { + url: body.url, + userId: session.user.id, + } + }) + + return NextResponse.json(link, { status: 201 }); +} |
