Skip to content

Commit

Permalink
Merge pull request #195 from FalkorDB/client-connection
Browse files Browse the repository at this point in the history
fix #194 refactor user authorization on server side
  • Loading branch information
Anchel123 authored May 1, 2024
2 parents 9a5e036 + c6e3bf3 commit d235e41
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 76 deletions.
40 changes: 26 additions & 14 deletions app/api/auth/[...nextauth]/options.ts
Original file line number Diff line number Diff line change
@@ -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<number, FalkorDB>();

Expand Down Expand Up @@ -35,19 +36,6 @@ async function newClient(credentials: {host: string, port: string, password: str
return client
}

export async function getConnection(user: User) : Promise<FalkorDB> {
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 = {
Expand Down Expand Up @@ -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
15 changes: 4 additions & 11 deletions app/api/graph/[graph]/[node]/route.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
39 changes: 10 additions & 29 deletions app/api/graph/[graph]/route.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
15 changes: 4 additions & 11 deletions app/api/graph/route.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand Down
15 changes: 4 additions & 11 deletions app/api/monitor/route.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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")
Expand Down

0 comments on commit d235e41

Please sign in to comment.