Skip to content

Commit

Permalink
using custom header for zeplo to pass clerk authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
PrinceBaghel258025 committed Nov 16, 2023
1 parent 773a4fb commit 1583dc2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 34 deletions.
21 changes: 4 additions & 17 deletions src/app/api/chatmodel/[chatid]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import { env } from "@/app/env.mjs";
export const revalidate = 0; // disable cache

export const maxDuration = 60;
import { auth } from "@clerk/nextjs";
import { cookies } from "next/headers";

export async function POST(
request: Request,
Expand All @@ -29,17 +27,6 @@ export async function POST(
let orgId = "";
orgId = body.orgId;
const url = request.url;
console.log("this is request url", url);
const { getToken } = await auth();
console.log("ckkoies", cookies());
console.log("token", await getToken());
const cookieStore = cookies();
const cookiesArray = cookieStore.getAll().map((cookie) => {
const cookieName = cookie.name;
const cookieValue = cookie.value;
return [cookieName, cookieValue] as [string, string];
});

// getting main url
const urlArray = url.split("/");

Expand Down Expand Up @@ -94,10 +81,10 @@ export async function POST(
{
method: "POST",
body: JSON.stringify({ chat: _chat }),
// headers: {
// Authorization: `Bearer ${await getToken()}`,
// },
headers: [...cookiesArray],
headers: {
"x-zeplo-secret": env.ZEPLO_SECRET,
},
// headers: [...cookiesArray],
},
);
await db
Expand Down
19 changes: 3 additions & 16 deletions src/app/api/generateTitle/[chatid]/[orgid]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { generateTitle } from "@/utils/apiHelper";
import { env } from "@/app/env.mjs";
import { ChatEntry } from "@/lib/types";
export const revalidate = 0; // disable cache
import { auth } from "@clerk/nextjs";
import { cookies } from "next/headers";
export async function POST(
request: Request,
params: { params: { chatid: string; orgid: string } },
Expand All @@ -16,16 +14,6 @@ export async function POST(
let orgId = params.params.orgid;
const body = await request.json();

const { getToken } = await auth();
console.log("ckkoies", cookies());
console.log("token", await getToken());
const cookieStore = cookies();
const cookiesArray = cookieStore.getAll().map((cookie) => {
const cookieName = cookie.name;
const cookieValue = cookie.value;
return [cookieName, cookieValue] as [string, string];
});

const messages: ChatEntry[] = body.chat;
const url = request.url;
const urlArray = url.split("/");
Expand All @@ -35,10 +23,9 @@ export async function POST(
{
method: "POST",
body: JSON.stringify({ chatTitle: fullResponse }),
// headers: {
// Authorization: `Bearer ${await getToken()}`,
// },
headers: cookiesArray,
headers: {
"z-zeplo-secret": env.ZEPLO_TOKEN,
},
},
);
await db
Expand Down
2 changes: 2 additions & 0 deletions src/app/env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const env = createEnv({
IMAGE_PREFIX_URL: z.string().min(1),
// zeplo (message queue)
ZEPLO_TOKEN: z.string().min(1),
ZEPLO_SECRET: z.string().min(1),
},

client: {
Expand Down Expand Up @@ -82,6 +83,7 @@ export const env = createEnv({
IMAGE_PREFIX_URL: process.env.IMAGE_PREFIX_URL,
// zeplo
ZEPLO_TOKEN: process.env.ZEPLO_TOKEN,
ZEPLO_SECRET: process.env.ZEPLO_SECRET,
},
});

30 changes: 29 additions & 1 deletion src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
import { authMiddleware } from "@clerk/nextjs";
export default authMiddleware({
import { NextRequest, NextResponse } from "next/server";
import { NextFetchEvent } from "next/server";

// This is your original middleware
const clerkMiddleware = authMiddleware({
// "/" will be accessible to all users
publicRoutes: ["/"],
});

export async function middleware(req: NextRequest, event: NextFetchEvent) {
console.log("got into middleware");
// Check if the request is from Zeplo
if (req.headers.get("x-zeplo-secret") === process.env.ZEPLO_SECRET) {
// Handle the request without Clerk's authMiddleware
console.log("got into if");
// handleRequest(req, res);
} else {
console.log("got into else of middleware");
// If not from Zeplo, use Clerk's authMiddleware
// const nextReq = new NextRequest(req);
// const nextFetchEvent = new NextFetchEvent(req);
return clerkMiddleware(req, event);
}
}

function handleRequest(req: NextRequest, res: NextResponse) {
// If you have a specific endpoint function, you can call it here.
// For example, if you have an endpoint function called 'endpointFunction':
// endpointFunction(req, res);
// If you don't have a specific function and just want to let the request meet the endpoint, you can do nothing here.
// The request will continue to the next middleware or the route handler.
}

export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};

0 comments on commit 1583dc2

Please sign in to comment.