:| finally
This commit is contained in:
parent
5ee4beb662
commit
c586903078
6 changed files with 47 additions and 20 deletions
|
@ -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 });
|
||||||
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
9
src/pages/api/external/ping.ts
vendored
9
src/pages/api/external/ping.ts
vendored
|
@ -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!" });
|
|
||||||
}
|
|
13
src/pages/api/private/ping.ts
Normal file
13
src/pages/api/private/ping.ts
Normal 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" }
|
||||||
|
);
|
||||||
|
}
|
13
src/pages/api/public/ping.ts
Normal file
13
src/pages/api/public/ping.ts
Normal 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" }
|
||||||
|
);
|
||||||
|
}
|
|
@ -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);
|
||||||
|
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue