Merge pull request #42 from atridadl/dev

2.0.2
This is a small one!
🚧 Re-structured the endpoints and added new middleware!
This commit is contained in:
Atridad Lahiji 2023-08-17 21:07:03 -06:00 committed by GitHub
commit b6430d82a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 57 additions and 34 deletions

View file

@ -1,6 +1,6 @@
{
"name": "sprintpadawan",
"version": "2.0.1",
"version": "2.0.2",
"description": "Plan. Sprint. Repeat.",
"private": true,
"scripts": {

View file

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

View file

@ -1,13 +0,0 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { validateRequest } from "~/server/unkey";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const isValid = await validateRequest(req, res);
if (isValid) {
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 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 function handler() {
return NextResponse.json(
{ message: "Public Pong!" },
{ status: 200, statusText: "SUCCESS" }
);
}

View file

@ -1,5 +1,4 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { validateRequest } from "~/server/unkey";
import {
onUserCreatedHandler,
onUserDeletedHandler,
@ -10,12 +9,6 @@ export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const isValid = await validateRequest(req, res);
if (!isValid) {
return;
}
try {
const requestBody = WebhookEventBodySchema.parse(req.body);

View file

@ -1,5 +1,5 @@
import { Unkey } from "@unkey/api";
import type { NextApiRequest, NextApiResponse } from "next";
import { NextRequest } from "next/server";
import { env } from "~/env.mjs";
export const unkey = new Unkey({ token: env.UNKEY_ROOT_KEY });
@ -15,22 +15,17 @@ export const validateApiKey = async (key: string) => {
}
};
export const validateRequest = async (
req: NextApiRequest,
res: NextApiResponse
) => {
export const validateRequest = async (req: NextRequest) => {
let isValidKey: boolean = false;
const authorization = req.headers.get("authorization");
// Get the auth bearer token if it exists
if (req.headers.authorization) {
const key = req.headers.authorization.split("Bearer ").at(1);
if (authorization) {
const key = authorization.split("Bearer ").at(1);
if (key) {
isValidKey = await validateApiKey(key);
}
}
if (!isValidKey) {
res.status(403).json({ error: "UNAUTHORIZED" });
}
return isValidKey;
};