mirror of
https://github.com/YuzuZensai/Minikura.git
synced 2026-09-14 03:09:50 +00:00
♻️ refactor: remove unused code
This commit is contained in:
@@ -1,57 +1,41 @@
|
||||
import type { User } from "@minikura/db";
|
||||
import type { Elysia } from "elysia";
|
||||
import { ForbiddenError, UnauthorizedError } from "../domain/errors/base.error";
|
||||
import { bearerToken, findApiKeyOwner } from "./api-key";
|
||||
|
||||
function authenticatedUser(ctx: { user?: User | null; isSuspended?: boolean }) {
|
||||
const { user, isSuspended } = ctx;
|
||||
if (!user) {
|
||||
throw new UnauthorizedError();
|
||||
}
|
||||
if (isSuspended) {
|
||||
throw new ForbiddenError("Account is suspended");
|
||||
}
|
||||
return { user };
|
||||
}
|
||||
|
||||
export const requireAuth = (app: Elysia) => {
|
||||
return app.derive((ctx: any) => {
|
||||
const { user, isSuspended } = ctx as {
|
||||
user: User | null;
|
||||
isSuspended: boolean;
|
||||
};
|
||||
if (!user) {
|
||||
throw new UnauthorizedError();
|
||||
}
|
||||
if (isSuspended) {
|
||||
throw new ForbiddenError("Account is suspended");
|
||||
}
|
||||
return { user };
|
||||
});
|
||||
};
|
||||
|
||||
export const requireAdmin = (app: Elysia) => {
|
||||
return app.derive((ctx: any) => {
|
||||
const { user, isSuspended } = ctx as {
|
||||
user: User | null;
|
||||
isSuspended: boolean;
|
||||
};
|
||||
if (!user) {
|
||||
throw new UnauthorizedError();
|
||||
}
|
||||
if (isSuspended) {
|
||||
throw new ForbiddenError("Account is suspended");
|
||||
}
|
||||
if (user.role !== "admin") {
|
||||
throw new ForbiddenError("Admin access required");
|
||||
}
|
||||
return { user };
|
||||
});
|
||||
return app.derive((ctx: any) => authenticatedUser(ctx));
|
||||
};
|
||||
|
||||
export const requireRole = (role: string) => (app: Elysia) => {
|
||||
return app.derive((ctx: any) => {
|
||||
const { user, isSuspended } = ctx as {
|
||||
user: User | null;
|
||||
isSuspended: boolean;
|
||||
};
|
||||
if (!user) {
|
||||
throw new UnauthorizedError();
|
||||
}
|
||||
if (isSuspended) {
|
||||
throw new ForbiddenError("Account is suspended");
|
||||
}
|
||||
const { user } = authenticatedUser(ctx);
|
||||
if (user.role !== role) {
|
||||
throw new ForbiddenError(`${role} access required`);
|
||||
}
|
||||
return { user };
|
||||
});
|
||||
};
|
||||
|
||||
export const requireAdmin = requireRole("admin");
|
||||
|
||||
export const requirePluginApiKey = (app: Elysia) => {
|
||||
return app.derive(async ({ request }) => {
|
||||
const owner = await findApiKeyOwner(bearerToken(request.headers.get("authorization")));
|
||||
if (!owner) {
|
||||
throw new UnauthorizedError();
|
||||
}
|
||||
return { pluginAuth: owner };
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
import type { z } from "zod";
|
||||
|
||||
type ErrorHandler = (code: number, value: unknown) => never;
|
||||
|
||||
export function validateBody<T extends z.ZodType>(
|
||||
schema: T,
|
||||
body: unknown,
|
||||
error: ErrorHandler
|
||||
): z.infer<T> {
|
||||
const result = schema.safeParse(body);
|
||||
|
||||
if (!result.success) {
|
||||
const firstError = result.error.issues[0];
|
||||
const message = `${firstError.path.join(".")}: ${firstError.message}`;
|
||||
throw error(400, { message });
|
||||
}
|
||||
|
||||
return result.data;
|
||||
}
|
||||
|
||||
export function zodValidate<T extends z.ZodType>(schema: T) {
|
||||
return (context: { body: unknown; error: ErrorHandler }) => {
|
||||
return validateBody(schema, context.body, context.error);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user