refactor: add authorization

This commit is contained in:
Mauricio Siu
2024-10-06 01:37:39 -06:00
parent 7cfbea3f60
commit 3cf27a068a
10 changed files with 191 additions and 33 deletions

View File

@@ -7,12 +7,28 @@ import { createClient } from "redis";
import { logger } from "./logger";
import { type DeployJob, deployJobSchema } from "./schema";
import { deploy } from "./utils";
import { validateBearerTokenAPI } from "@dokploy/server";
const app = new Hono();
const redisClient = createClient({
url: process.env.REDIS_URL,
});
app.use(async (c, next) => {
const authHeader = c.req.header("authorization");
if (!authHeader || !authHeader.startsWith("Bearer ")) {
return c.json({ message: "Authorization header missing" }, 401);
}
const result = await validateBearerTokenAPI(authHeader);
if (!result.user || !result.session) {
return c.json({ message: "Invalid session" }, 403);
}
return next();
});
app.post("/deploy", zValidator("json", deployJobSchema), (c) => {
const data = c.req.valid("json");
const res = queue.add(data, { groupName: data.serverId });