aboutsummaryrefslogtreecommitdiffhomepage
path: root/pkgs/server/src/middleware/auth.ts
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-12-06 17:05:21 +0900
committernsfisis <nsfisis@gmail.com>2025-12-06 17:37:04 +0900
commit811458427593a4172a2cd535cc768db375350dca (patch)
tree6c4f46c96b6f29392dc19d591e39e03c187033a1 /pkgs/server/src/middleware/auth.ts
parent9736a8981fbd6c6defbd67517ca23904fc844629 (diff)
downloadkioku-811458427593a4172a2cd535cc768db375350dca.tar.gz
kioku-811458427593a4172a2cd535cc768db375350dca.tar.zst
kioku-811458427593a4172a2cd535cc768db375350dca.zip
feat(dev): change architecture and directory structure
Diffstat (limited to 'pkgs/server/src/middleware/auth.ts')
-rw-r--r--pkgs/server/src/middleware/auth.ts65
1 files changed, 0 insertions, 65 deletions
diff --git a/pkgs/server/src/middleware/auth.ts b/pkgs/server/src/middleware/auth.ts
deleted file mode 100644
index c295834..0000000
--- a/pkgs/server/src/middleware/auth.ts
+++ /dev/null
@@ -1,65 +0,0 @@
-import type { Context, Next } from "hono";
-import { verify } from "hono/jwt";
-import { Errors } from "./error-handler";
-
-const JWT_SECRET = process.env.JWT_SECRET;
-if (!JWT_SECRET) {
- throw new Error("JWT_SECRET environment variable is required");
-}
-
-export interface AuthUser {
- id: string;
-}
-
-interface JWTPayload {
- sub: string;
- iat: number;
- exp: number;
-}
-
-/**
- * Auth middleware that validates JWT tokens from Authorization header
- * Sets the authenticated user in context variables
- */
-export async function authMiddleware(c: Context, next: Next) {
- const authHeader = c.req.header("Authorization");
-
- if (!authHeader) {
- throw Errors.unauthorized("Missing Authorization header", "MISSING_AUTH");
- }
-
- if (!authHeader.startsWith("Bearer ")) {
- throw Errors.unauthorized(
- "Invalid Authorization header format",
- "INVALID_AUTH_FORMAT",
- );
- }
-
- const token = authHeader.slice(7);
-
- try {
- const payload = (await verify(token, JWT_SECRET)) as unknown as JWTPayload;
-
- const user: AuthUser = {
- id: payload.sub,
- };
-
- c.set("user", user);
-
- await next();
- } catch {
- throw Errors.unauthorized("Invalid or expired token", "INVALID_TOKEN");
- }
-}
-
-/**
- * Helper function to get the authenticated user from context
- * Throws if user is not authenticated
- */
-export function getAuthUser(c: Context): AuthUser {
- const user = c.get("user") as AuthUser | undefined;
- if (!user) {
- throw Errors.unauthorized("Not authenticated", "NOT_AUTHENTICATED");
- }
- return user;
-}