pollo/src/server/unkey.ts

25 lines
658 B
TypeScript
Raw Normal View History

2023-08-23 17:50:17 -06:00
// import { Unkey, verifyKey } from "@unkey/api";
import { verifyKey } from "@unkey/api";
2023-08-20 17:20:24 -06:00
import type { NextRequest } from "next/server";
2023-08-23 17:50:17 -06:00
// import { env } from "~/env.mjs";
2023-08-06 14:09:03 -06:00
2023-08-23 17:50:17 -06:00
// const unkey = new Unkey({token: env.UNKEY_ROOT_KEY})
2023-08-14 01:18:35 -06:00
2023-08-17 16:40:09 -06:00
export const validateRequest = async (req: NextRequest) => {
const authorization = req.headers.get("authorization");
2023-08-14 01:18:35 -06:00
// Get the auth bearer token if it exists
2023-08-17 16:40:09 -06:00
if (authorization) {
const key = authorization.split("Bearer ").at(1);
2023-08-14 01:18:35 -06:00
if (key) {
2023-08-23 17:50:17 -06:00
const { error, result } = await verifyKey(key);
if (!error) {
console.log(result);
return result.valid;
}
2023-08-14 01:18:35 -06:00
}
}
2023-08-23 17:50:17 -06:00
return false;
2023-08-14 01:18:35 -06:00
};