mirror of
https://github.com/khairul169/home-lab.git
synced 2026-10-02 17:13:57 +07:00
feat: add file manager app
This commit is contained in:
+36
-1
@@ -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();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user