From 2acd00ac02c696f07c711f6750ee1a8849d91e2f Mon Sep 17 00:00:00 2001 From: Bram Adams <3282661+bramses@users.noreply.github.com> Date: Fri, 23 Aug 2024 08:56:05 -0400 Subject: [PATCH] feat: Add API_KEY to search route for authentication --- src/app/[locale]/(auth)/api/add/route.ts | 11 ++++++++++- src/app/[locale]/(auth)/api/daily/route.ts | 10 +++++++++- src/app/[locale]/(auth)/api/getCBPath/route.ts | 14 ++++++++++++++ src/app/[locale]/(auth)/api/list/route.ts | 10 +++++++++- src/app/[locale]/(auth)/api/random/route.ts | 12 ++++++++++-- src/app/[locale]/(auth)/api/search/route.ts | 13 ++++++++++++- src/app/[locale]/(auth)/api/update/route.ts | 14 ++++++++++---- src/components/SearchBox.tsx | 10 +++++++++- src/middleware.ts | 1 + 9 files changed, 84 insertions(+), 11 deletions(-) create mode 100644 src/app/[locale]/(auth)/api/getCBPath/route.ts diff --git a/src/app/[locale]/(auth)/api/add/route.ts b/src/app/[locale]/(auth)/api/add/route.ts index 7841075..210671a 100644 --- a/src/app/[locale]/(auth)/api/add/route.ts +++ b/src/app/[locale]/(auth)/api/add/route.ts @@ -2,11 +2,19 @@ import { NextResponse } from 'next/server'; import { logger } from '@/libs/Logger'; +import { GET } from '../getCBPath/route'; + // import env variables export const POST = async (request: Request) => { const { data, metadata } = await request.json(); - const { CLOUD_URL, DATABASE_URL } = process.env; + const { CLOUD_URL } = process.env; + + const dbRes = await GET(request); + if (!dbRes) { + return NextResponse.json({}, { status: 500 }); + } + const { DATABASE_URL, API_KEY } = await dbRes.json(); const resp = await fetch(`${CLOUD_URL}/add`, { method: 'POST', @@ -17,6 +25,7 @@ export const POST = async (request: Request) => { dbPath: DATABASE_URL, data, metadata, + apiKey: API_KEY, }), }); logger.info('resp:', resp); diff --git a/src/app/[locale]/(auth)/api/daily/route.ts b/src/app/[locale]/(auth)/api/daily/route.ts index 5b5647b..1736ea9 100644 --- a/src/app/[locale]/(auth)/api/daily/route.ts +++ b/src/app/[locale]/(auth)/api/daily/route.ts @@ -2,11 +2,19 @@ import { NextResponse } from 'next/server'; import { logger } from '@/libs/Logger'; +import { GET } from '../getCBPath/route'; + // import env variables export const POST = async (request: Request) => { const { date } = await request.json(); - const { CLOUD_URL, DATABASE_URL } = process.env; + const { CLOUD_URL } = process.env; + + const dbRes = await GET(request); + if (!dbRes) { + return NextResponse.json({}, { status: 500 }); + } + const { DATABASE_URL } = await dbRes.json(); const resp = await fetch(`${CLOUD_URL}/entriesByDate`, { method: 'POST', diff --git a/src/app/[locale]/(auth)/api/getCBPath/route.ts b/src/app/[locale]/(auth)/api/getCBPath/route.ts new file mode 100644 index 0000000..a2c3616 --- /dev/null +++ b/src/app/[locale]/(auth)/api/getCBPath/route.ts @@ -0,0 +1,14 @@ +import { auth, clerkClient } from '@clerk/nextjs/server'; +import { NextResponse } from 'next/server'; + +export async function GET(_: Request) { + const { userId }: { userId: string | null } = auth(); + + if (!userId) return null; + + const user = await clerkClient.users.getUser(userId); + return NextResponse.json({ + DATABASE_URL: user.privateMetadata.cbPath, + API_KEY: user.privateMetadata.apiKey, + }); +} diff --git a/src/app/[locale]/(auth)/api/list/route.ts b/src/app/[locale]/(auth)/api/list/route.ts index c1b8eb5..13fe891 100644 --- a/src/app/[locale]/(auth)/api/list/route.ts +++ b/src/app/[locale]/(auth)/api/list/route.ts @@ -2,11 +2,19 @@ import { NextResponse } from 'next/server'; import { logger } from '@/libs/Logger'; +import { GET } from '../getCBPath/route'; + // import env variables export const POST = async (request: Request) => { const { page, limit, sortModel, filterModel } = await request.json(); - const { CLOUD_URL, DATABASE_URL } = process.env; + const { CLOUD_URL } = process.env; + + const dbRes = await GET(request); + if (!dbRes) { + return NextResponse.json({}, { status: 500 }); + } + const { DATABASE_URL } = await dbRes.json(); const resp = await fetch(`${CLOUD_URL}/list`, { method: 'POST', diff --git a/src/app/[locale]/(auth)/api/random/route.ts b/src/app/[locale]/(auth)/api/random/route.ts index 5eb728c..e6d4707 100644 --- a/src/app/[locale]/(auth)/api/random/route.ts +++ b/src/app/[locale]/(auth)/api/random/route.ts @@ -2,10 +2,18 @@ import { NextResponse } from 'next/server'; import { logger } from '@/libs/Logger'; +import { GET } from '../getCBPath/route'; + // import env variables -export const POST = async (_: Request) => { - const { CLOUD_URL, DATABASE_URL } = process.env; +export const POST = async (request: Request) => { + const { CLOUD_URL } = process.env; + + const dbRes = await GET(request); + if (!dbRes) { + return NextResponse.json({}, { status: 500 }); + } + const { DATABASE_URL } = await dbRes.json(); const resp = await fetch(`${CLOUD_URL}/random`, { method: 'POST', diff --git a/src/app/[locale]/(auth)/api/search/route.ts b/src/app/[locale]/(auth)/api/search/route.ts index 6022b4e..065a8f1 100644 --- a/src/app/[locale]/(auth)/api/search/route.ts +++ b/src/app/[locale]/(auth)/api/search/route.ts @@ -2,11 +2,21 @@ import { NextResponse } from 'next/server'; import { logger } from '@/libs/Logger'; +import { GET } from '../getCBPath/route'; + // import env variables export const POST = async (request: Request) => { const { query } = await request.json(); - const { CLOUD_URL, DATABASE_URL } = process.env; + const { CLOUD_URL } = process.env; + + const dbRes = await GET(request); + if (!dbRes) { + return NextResponse.json({}, { status: 500 }); + } + const { DATABASE_URL, API_KEY } = await dbRes.json(); + console.log('DATABASE_URL:', DATABASE_URL); + console.log('API_KEY:', API_KEY); const resp = await fetch(`${CLOUD_URL}/search`, { method: 'POST', @@ -16,6 +26,7 @@ export const POST = async (request: Request) => { body: JSON.stringify({ query, dbPath: DATABASE_URL, + apiKey: API_KEY, }), }); diff --git a/src/app/[locale]/(auth)/api/update/route.ts b/src/app/[locale]/(auth)/api/update/route.ts index 555b223..8779ded 100644 --- a/src/app/[locale]/(auth)/api/update/route.ts +++ b/src/app/[locale]/(auth)/api/update/route.ts @@ -2,14 +2,19 @@ import { NextResponse } from 'next/server'; import { logger } from '@/libs/Logger'; +import { GET } from '../getCBPath/route'; + // import env variables export const POST = async (request: Request) => { const { id, data, metadata } = await request.json(); - const { CLOUD_URL, DATABASE_URL } = process.env; - console.log('id:', id); - console.log('data:', data); - console.log('metadata:', metadata); + const { CLOUD_URL } = process.env; + + const dbRes = await GET(request); + if (!dbRes) { + return NextResponse.json({}, { status: 500 }); + } + const { DATABASE_URL, API_KEY } = await dbRes.json(); const resp = await fetch(`${CLOUD_URL}/update`, { method: 'POST', @@ -21,6 +26,7 @@ export const POST = async (request: Request) => { dbPath: DATABASE_URL, data, metadata, + apiKey: API_KEY, }), }); logger.info('resp:', resp); diff --git a/src/components/SearchBox.tsx b/src/components/SearchBox.tsx index a813d81..aa07fcc 100644 --- a/src/components/SearchBox.tsx +++ b/src/components/SearchBox.tsx @@ -2,7 +2,7 @@ 'use client'; -import { useState } from 'react'; +import { useEffect, useState } from 'react'; import { getCache, invalidateCache, setCache } from '@/helpers/cache'; @@ -16,6 +16,14 @@ const SearchBox = () => { const [collection, setCollection] = useState([]); const cache = getCache(); + useEffect(() => { + fetch('/api/getCBPath') + .then((res) => res.json()) + .then((data) => { + console.log('data:', data); + }); + }, [searchResults]); + const fetchByID = async (id: string) => { const cachedAlias = cache.aliases[id]; if (cachedAlias) { diff --git a/src/middleware.ts b/src/middleware.ts index 6596815..a074a70 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -13,6 +13,7 @@ const intlMiddleware = createMiddleware({ const isProtectedRoute = createRouteMatcher([ '/dashboard(.*)', '/:locale/dashboard(.*)', + '/api/(.*)', ]); export default function middleware(