Skip to content

Commit

Permalink
feat: Add API_KEY to search route for authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
bramses committed Aug 23, 2024
1 parent 989f50f commit 2acd00a
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 11 deletions.
11 changes: 10 additions & 1 deletion src/app/[locale]/(auth)/api/add/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -17,6 +25,7 @@ export const POST = async (request: Request) => {
dbPath: DATABASE_URL,
data,
metadata,
apiKey: API_KEY,
}),
});
logger.info('resp:', resp);
Expand Down
10 changes: 9 additions & 1 deletion src/app/[locale]/(auth)/api/daily/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
14 changes: 14 additions & 0 deletions src/app/[locale]/(auth)/api/getCBPath/route.ts
Original file line number Diff line number Diff line change
@@ -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,
});
}
10 changes: 9 additions & 1 deletion src/app/[locale]/(auth)/api/list/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
12 changes: 10 additions & 2 deletions src/app/[locale]/(auth)/api/random/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
13 changes: 12 additions & 1 deletion src/app/[locale]/(auth)/api/search/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -16,6 +26,7 @@ export const POST = async (request: Request) => {
body: JSON.stringify({
query,
dbPath: DATABASE_URL,
apiKey: API_KEY,
}),
});

Expand Down
14 changes: 10 additions & 4 deletions src/app/[locale]/(auth)/api/update/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -21,6 +26,7 @@ export const POST = async (request: Request) => {
dbPath: DATABASE_URL,
data,
metadata,
apiKey: API_KEY,
}),
});
logger.info('resp:', resp);
Expand Down
10 changes: 9 additions & 1 deletion src/components/SearchBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

'use client';

import { useState } from 'react';
import { useEffect, useState } from 'react';

import { getCache, invalidateCache, setCache } from '@/helpers/cache';

Expand All @@ -16,6 +16,14 @@ const SearchBox = () => {
const [collection, setCollection] = useState<any[]>([]);
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) {
Expand Down
1 change: 1 addition & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const intlMiddleware = createMiddleware({
const isProtectedRoute = createRouteMatcher([
'/dashboard(.*)',
'/:locale/dashboard(.*)',
'/api/(.*)',
]);

export default function middleware(
Expand Down

0 comments on commit 2acd00a

Please sign in to comment.