Ping + streamlined validation of keys
This commit is contained in:
parent
22d37b8f0a
commit
0975a7aa84
4 changed files with 56 additions and 36 deletions
15
src/pages/api/external/ping.ts
vendored
Normal file
15
src/pages/api/external/ping.ts
vendored
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||||||
|
import { db } from "~/server/db";
|
||||||
|
import { validateRequest } from "~/server/unkey";
|
||||||
|
|
||||||
|
export default async function handler(
|
||||||
|
req: NextApiRequest,
|
||||||
|
res: NextApiResponse
|
||||||
|
) {
|
||||||
|
const success = await validateRequest(req, res);
|
||||||
|
|
||||||
|
if (success) {
|
||||||
|
await db.query.votes.findFirst();
|
||||||
|
res.status(200).json({ result: "Pong!" });
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +0,0 @@
|
||||||
|
|
||||||
import type { NextApiRequest, NextApiResponse } from "next";
|
|
||||||
|
|
||||||
export default function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
||||||
res.status(200).json({ result: "Pong!" });
|
|
||||||
}
|
|
|
@ -2,45 +2,34 @@ import { eq } from "drizzle-orm";
|
||||||
import type { NextApiRequest, NextApiResponse } from "next";
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||||||
import { db } from "~/server/db";
|
import { db } from "~/server/db";
|
||||||
import { logs, rooms, votes } from "~/server/schema";
|
import { logs, rooms, votes } from "~/server/schema";
|
||||||
import { validateApiKey } from "~/server/unkey";
|
import { validateApiKey, validateRequest } from "~/server/unkey";
|
||||||
|
|
||||||
export default async function handler(
|
export default async function handler(
|
||||||
req: NextApiRequest,
|
req: NextApiRequest,
|
||||||
res: NextApiResponse
|
res: NextApiResponse
|
||||||
) {
|
) {
|
||||||
let isValidKey: boolean = false;
|
const success = await validateRequest(req, res);
|
||||||
|
|
||||||
// Get the auth bearer token if it exists
|
if (success) {
|
||||||
if (req.headers.authorization) {
|
const requestBody = req.body as {
|
||||||
const key = req.headers.authorization.split("Bearer ").at(1);
|
data: {
|
||||||
if (key) {
|
deleted: string;
|
||||||
isValidKey = await validateApiKey(key);
|
id: string;
|
||||||
}
|
object: string;
|
||||||
}
|
};
|
||||||
|
|
||||||
// Error if the key is not valid
|
|
||||||
if (!isValidKey) {
|
|
||||||
res.status(403).json({ error: "UNAUTHORIZED" });
|
|
||||||
}
|
|
||||||
|
|
||||||
const requestBody = req.body as {
|
|
||||||
data: {
|
|
||||||
deleted: string;
|
|
||||||
id: string;
|
|
||||||
object: string;
|
object: string;
|
||||||
|
type: string;
|
||||||
};
|
};
|
||||||
object: string;
|
|
||||||
type: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
const deletedRoom = await db
|
const deletedRoom = await db
|
||||||
.delete(rooms)
|
.delete(rooms)
|
||||||
.where(eq(rooms.userId, requestBody.data.id));
|
.where(eq(rooms.userId, requestBody.data.id));
|
||||||
|
|
||||||
if (deletedRoom.rowsAffected > 0) {
|
if (deletedRoom.rowsAffected > 0) {
|
||||||
await db.delete(logs).where(eq(logs.userId, requestBody.data.id));
|
await db.delete(logs).where(eq(logs.userId, requestBody.data.id));
|
||||||
await db.delete(votes).where(eq(votes.userId, requestBody.data.id));
|
await db.delete(votes).where(eq(votes.userId, requestBody.data.id));
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(200).json({ result: "USER DELETED" });
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(200).json({ result: "USER DELETED" });
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import { Unkey } from "@unkey/api";
|
import { Unkey } from "@unkey/api";
|
||||||
|
import { NextApiRequest, NextApiResponse } from "next";
|
||||||
import { env } from "~/env.mjs";
|
import { env } from "~/env.mjs";
|
||||||
|
|
||||||
export const unkey = new Unkey({ token: env.UNKEY_ROOT_KEY });
|
export const unkey = new Unkey({ token: env.UNKEY_ROOT_KEY });
|
||||||
|
@ -13,3 +14,24 @@ export const validateApiKey = async (key: string) => {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const validateRequest = async (
|
||||||
|
req: NextApiRequest,
|
||||||
|
res: NextApiResponse
|
||||||
|
) => {
|
||||||
|
let isValidKey: boolean = false;
|
||||||
|
// Get the auth bearer token if it exists
|
||||||
|
if (req.headers.authorization) {
|
||||||
|
const key = req.headers.authorization.split("Bearer ").at(1);
|
||||||
|
if (key) {
|
||||||
|
isValidKey = await validateApiKey(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error if the key is not valid
|
||||||
|
if (!isValidKey) {
|
||||||
|
res.status(403).json({ error: "UNAUTHORIZED" });
|
||||||
|
}
|
||||||
|
|
||||||
|
return isValidKey;
|
||||||
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue