:| finally

This commit is contained in:
Atridad Lahiji 2023-08-17 20:55:56 -06:00
parent 5ee4beb662
commit c586903078
No known key found for this signature in database
6 changed files with 47 additions and 20 deletions

View file

@ -1,18 +1,33 @@
import { authMiddleware } from "@clerk/nextjs"; import { authMiddleware, redirectToSignIn } from "@clerk/nextjs";
import { validateRequest } from "./server/unkey"; import { validateRequest } from "./server/unkey";
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
export default authMiddleware({ export default authMiddleware({
publicRoutes: ["/", "/api/(.*)"], publicRoutes: ["/", "/api/public/(.*)"],
beforeAuth: async (req) => { afterAuth: async (auth, req) => {
if (req.nextUrl.pathname.startsWith("/api/external")) { if (!auth.userId && auth.isPublicRoute) {
console.log("1");
return NextResponse.next();
}
if (
req.nextUrl.pathname.includes("/api/webhooks") ||
req.nextUrl.pathname.includes("/api/private")
) {
console.log("2");
const isValid = await validateRequest(req); const isValid = await validateRequest(req);
console.log("Is Valid?: ", isValid); console.log("Is Valid?: ", isValid);
if (isValid) { if (isValid) {
return NextResponse.next(); return NextResponse.next();
} } else {
return new NextResponse("UNAUTHORIZED", { status: 403 }); return new NextResponse("UNAUTHORIZED", { status: 403 });
} }
}
if (!auth.userId && !auth.isPublicRoute) {
console.log(req.nextUrl);
console.log("3");
return redirectToSignIn({ returnBackUrl: req.url });
}
}, },
}); });

View file

@ -1,9 +0,0 @@
import type { NextApiRequest, NextApiResponse } from "next";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
console.log("Made it to the function!");
res.status(200).json({ result: "Pong!" });
}

View file

@ -0,0 +1,13 @@
import { NextResponse } from "next/server";
export const config = {
runtime: "edge",
regions: ["pdx1"],
};
export default async function handler() {
return NextResponse.json(
{ message: "Private Pong!" },
{ status: 200, statusText: "SUCCESS" }
);
}

View file

@ -0,0 +1,13 @@
import { NextResponse } from "next/server";
export const config = {
runtime: "edge",
regions: ["pdx1"],
};
export default async function handler() {
return NextResponse.json(
{ message: "Public Pong!" },
{ status: 200, statusText: "SUCCESS" }
);
}

View file

@ -10,12 +10,6 @@ export default async function handler(
req: NextApiRequest, req: NextApiRequest,
res: NextApiResponse res: NextApiResponse
) { ) {
const isValid = await validateRequest(req, res);
if (!isValid) {
return;
}
try { try {
const requestBody = WebhookEventBodySchema.parse(req.body); const requestBody = WebhookEventBodySchema.parse(req.body);

View file

@ -17,6 +17,7 @@ export const validateApiKey = async (key: string) => {
export const validateRequest = async (req: NextRequest) => { export const validateRequest = async (req: NextRequest) => {
let isValidKey: boolean = false; let isValidKey: boolean = false;
const authorization = req.headers.get("authorization"); const authorization = req.headers.get("authorization");
// Get the auth bearer token if it exists // Get the auth bearer token if it exists
if (authorization) { if (authorization) {