aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/api/auth/[...nextauth]/route.tsx22
-rw-r--r--app/api/v1/links/route.ts28
-rw-r--r--lib/auth.ts25
-rw-r--r--lib/prisma.ts5
-rw-r--r--lib/types/next-auth.d.ts12
5 files changed, 72 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 });
+}
diff --git a/lib/auth.ts b/lib/auth.ts
new file mode 100644
index 00000000..9b21e605
--- /dev/null
+++ b/lib/auth.ts
@@ -0,0 +1,25 @@
+import NextAuth, { NextAuthOptions } from "next-auth"
+import { PrismaAdapter } from "@next-auth/prisma-adapter"
+import AuthentikProvider from "next-auth/providers/authentik";
+import serverConfig from "@/lib/config";
+import prisma from "@/lib/prisma";
+
+let providers = [];
+
+if (serverConfig.auth.authentik) {
+ providers.push(AuthentikProvider(serverConfig.auth.authentik));
+}
+
+export const authOptions: NextAuthOptions = {
+ // Configure one or more authentication providers
+ adapter: PrismaAdapter(prisma),
+ providers: providers,
+ callbacks: {
+ session({ session, token, user }) {
+ session.user = { ...user };
+ return session;
+ }
+ }
+};
+
+export const authHandler = NextAuth(authOptions);
diff --git a/lib/prisma.ts b/lib/prisma.ts
new file mode 100644
index 00000000..d73ba5f2
--- /dev/null
+++ b/lib/prisma.ts
@@ -0,0 +1,5 @@
+import { PrismaClient } from '@prisma/client'
+
+const prisma = new PrismaClient();
+
+export default prisma;
diff --git a/lib/types/next-auth.d.ts b/lib/types/next-auth.d.ts
new file mode 100644
index 00000000..bdd3bd03
--- /dev/null
+++ b/lib/types/next-auth.d.ts
@@ -0,0 +1,12 @@
+import NextAuth, { DefaultSession } from "next-auth"
+
+declare module "next-auth" {
+ /**
+ * Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
+ */
+ interface Session {
+ user: {
+ id: string;
+ } & DefaultSession["user"];
+ }
+}