feat: add file manager app

This commit is contained in:
2024-03-16 16:03:15 +07:00
parent 9eaf90ee2f
commit b1216df09e
18 changed files with 434 additions and 13 deletions
+36 -1
View File
@@ -1,3 +1,5 @@
import type { Context, Next } from "hono";
import { HTTPException } from "hono/http-exception";
import * as jwt from "hono/jwt";
const JWT_SECRET =
@@ -11,4 +13,37 @@ export const verifyToken = async (token: string) => {
return jwt.verify(token, JWT_SECRET);
};
export const authMiddleware = jwt.jwt({ secret: JWT_SECRET });
export const authMiddleware = async (ctx: Context, next: Next) => {
const authHeader = ctx.req.raw.headers.get("Authorization");
const queryToken = ctx.req.query("token");
let token;
if (authHeader) {
const parts = authHeader.split(/\s+/);
if (parts.length !== 2) {
throw new HTTPException(401, { message: "Unauthorized!" });
} else {
token = parts[1];
}
}
if (queryToken) {
token = queryToken;
}
if (!token) {
throw new HTTPException(401, { message: "Unauthorized!" });
}
let payload;
try {
payload = await jwt.verify(token, JWT_SECRET);
} catch (e) {}
if (!payload) {
throw new HTTPException(401, { message: "Unauthorized!" });
}
ctx.set("jwtPayload", payload);
await next();
};