From 869805cac50050c6b82d631a280722f3c18febb6 Mon Sep 17 00:00:00 2001 From: Guy Korland Date: Mon, 29 Apr 2024 15:34:26 +0300 Subject: [PATCH] fix #194 refactor user authorization on server side --- app/api/auth/[...nextauth]/options.ts | 40 +++++++++++++++++---------- app/api/graph/[graph]/[node]/route.ts | 15 +++------- app/api/graph/[graph]/route.ts | 39 +++++++------------------- app/api/graph/route.ts | 15 +++------- app/api/monitor/route.ts | 15 +++------- 5 files changed, 48 insertions(+), 76 deletions(-) diff --git a/app/api/auth/[...nextauth]/options.ts b/app/api/auth/[...nextauth]/options.ts index 9e09e51b..b1291d35 100644 --- a/app/api/auth/[...nextauth]/options.ts +++ b/app/api/auth/[...nextauth]/options.ts @@ -1,6 +1,7 @@ import { FalkorDB } from "falkordb"; import CredentialsProvider from "next-auth/providers/credentials" -import { AuthOptions, User } from "next-auth" +import { AuthOptions, getServerSession } from "next-auth" +import { NextResponse } from "next/server"; const connections = new Map(); @@ -35,19 +36,6 @@ async function newClient(credentials: {host: string, port: string, password: str return client } -export async function getConnection(user: User) : Promise { - let conn = connections.get(user.id) - if (!conn) { - conn = await newClient({ - host: user.host, - port: user.port.toString() ?? "6379", - username: user.username, - password: user.password, - }, user.id) - } - return conn -} - let userId = 1; const authOptions: AuthOptions = { @@ -121,6 +109,30 @@ const authOptions: AuthOptions = { } } +export async function getClient() { + const session = await getServerSession(authOptions) + const id = session?.user?.id + if(!id) { + return NextResponse.json({ message: "Not authenticated" }, { status: 401 }) + } + + const { user } = session; + let client = connections.get(user.id) + // If client is not found, create a new one + if (!client) { + client = await newClient({ + host: user.host, + port: user.port.toString() ?? "6379", + username: user.username, + password: user.password, + }, user.id) + } + + if(!client) { + return NextResponse.json({ message: "Not authenticated" }, { status: 401 }) + } + return client +} export default authOptions \ No newline at end of file diff --git a/app/api/graph/[graph]/[node]/route.ts b/app/api/graph/[graph]/[node]/route.ts index 97edf693..34f849ae 100644 --- a/app/api/graph/[graph]/[node]/route.ts +++ b/app/api/graph/[graph]/[node]/route.ts @@ -1,19 +1,12 @@ import { NextRequest, NextResponse } from "next/server"; -import { getServerSession } from "next-auth/next"; -import authOptions, { getConnection } from "../../../auth/[...nextauth]/options"; +import { getClient } from "../../../auth/[...nextauth]/options"; // eslint-disable-next-line import/prefer-default-export export async function GET(request: NextRequest, { params }: { params: { graph: string, node: string } }) { - const session = await getServerSession(authOptions) - const id = session?.user?.id - if (!id) { - return NextResponse.json({ message: "Not authenticated" }, { status: 401 }) - } - - const client = await getConnection(session.user) - if (!client) { - return NextResponse.json({ message: "Not authenticated" }, { status: 401 }) + const client = await getClient() + if (client instanceof NextResponse) { + return client } const nodeId = parseInt(params.node, 10); diff --git a/app/api/graph/[graph]/route.ts b/app/api/graph/[graph]/route.ts index 48be8b2b..a4d31dbd 100644 --- a/app/api/graph/[graph]/route.ts +++ b/app/api/graph/[graph]/route.ts @@ -1,19 +1,12 @@ import { NextRequest, NextResponse } from "next/server"; -import { getServerSession } from "next-auth/next"; -import authOptions, { getConnection } from "../../auth/[...nextauth]/options"; +import { getClient } from "../../auth/[...nextauth]/options"; // eslint-disable-next-line import/prefer-default-export export async function DELETE(request: NextRequest, { params }: { params: { graph: string } }) { - const session = await getServerSession(authOptions) - const id = session?.user?.id - if (!id) { - return NextResponse.json({ message: "Not authenticated" }, { status: 401 }) - } - - const client = await getConnection(session.user) - if (!client) { - return NextResponse.json({ message: "Not authenticated" }, { status: 401 }) + const client = await getClient() + if (client instanceof NextResponse) { + return client } const graphId = params.graph; @@ -35,15 +28,9 @@ export async function DELETE(request: NextRequest, { params }: { params: { graph // eslint-disable-next-line import/prefer-default-export export async function POST(request: NextRequest, { params }: { params: { graph: string } }) { - const session = await getServerSession(authOptions) - const id = session?.user?.id - if (!id) { - return NextResponse.json({ message: "Not authenticated" }, { status: 401 }) - } - - const client = await getConnection(session.user) - if (!client) { - return NextResponse.json({ message: "Not authenticated" }, { status: 401 }) + const client = await getClient() + if (client instanceof NextResponse) { + return client } const graphId = params.graph; @@ -65,15 +52,9 @@ export async function POST(request: NextRequest, { params }: { params: { graph: // eslint-disable-next-line import/prefer-default-export export async function PATCH(request: NextRequest, { params }: { params: { graph: string } }) { - const session = await getServerSession(authOptions) - const id = session?.user?.id - if (!id) { - return NextResponse.json({ message: "Not authenticated" }, { status: 401 }) - } - - const client = await getConnection(session.user) - if (!client) { - return NextResponse.json({ message: "Not authenticated" }, { status: 401 }) + const client = await getClient() + if (client instanceof NextResponse) { + return client } const graphId = params.graph; diff --git a/app/api/graph/route.ts b/app/api/graph/route.ts index 86e9cf51..0aacdb08 100644 --- a/app/api/graph/route.ts +++ b/app/api/graph/route.ts @@ -1,19 +1,12 @@ import { NextRequest, NextResponse } from "next/server"; -import { getServerSession } from "next-auth/next"; -import authOptions, { getConnection } from "../auth/[...nextauth]/options"; +import { getClient } from "../auth/[...nextauth]/options"; // eslint-disable-next-line import/prefer-default-export export async function GET(request: NextRequest) { - const session = await getServerSession(authOptions) - const id = session?.user?.id - if(!id) { - return NextResponse.json({ message: "Not authenticated" }, { status: 401 }) - } - - const client = await getConnection(session.user) - if(!client) { - return NextResponse.json({ message: "Not authenticated" }, { status: 401 }) + const client = await getClient() + if (client instanceof NextResponse) { + return client } const graphID = request.nextUrl.searchParams.get("graph"); diff --git a/app/api/monitor/route.ts b/app/api/monitor/route.ts index 9192bf6f..5e08ea76 100644 --- a/app/api/monitor/route.ts +++ b/app/api/monitor/route.ts @@ -1,6 +1,5 @@ import { NextResponse } from "next/server"; -import { getServerSession } from "next-auth/next"; -import authOptions, { getConnection } from "../auth/[...nextauth]/options"; +import { getClient } from "../auth/[...nextauth]/options"; const fileds = [ "used_memory", @@ -9,15 +8,9 @@ const fileds = [ // eslint-disable-next-line import/prefer-default-export export async function GET() { - const session = await getServerSession(authOptions) - const id = session?.user?.id - if (!id) { - return NextResponse.json({ message: "Not authenticated" }, { status: 401 }) - } - - const client = await getConnection(session.user) - if (!client) { - return NextResponse.json({ message: "Not authenticated" }, { status: 401 }) + const client = await getClient() + if (client instanceof NextResponse) { + return client } const infoMemory = await client.connection.info("memory")