import { banUserSchema, checkUsernameSchema, ensureAccountExistsSchema, rotatePasswordSchema, } from "./data"; import { HonoContext } from "@core/hono.helpers"; import { sValidator } from "@hono/standard-validator"; import { getUserController } from "./controller"; import { Hono } from "hono"; const uc = getUserController(); export const usersRouter = new Hono() // Get current user info .get("/me", async (c) => { const fctx = c.env.locals.fCtx; const userId = c.env.locals.user?.id; if (!userId) { return c.json( { error: { code: "UNAUTHORIZED", message: "User not authenticated", description: "Please log in", detail: "No user ID found in session", }, }, 401, ); } const res = await uc.getUserInfo(fctx, userId); return c.json( res.isOk() ? { data: res.value, error: null } : { data: null, error: res.error }, res.isOk() ? 200 : 400, ); }) // Get user info by ID .get("/:userId", async (c) => { const fctx = c.env.locals.fCtx; const userId = c.req.param("userId"); const res = await uc.getUserInfo(fctx, userId); return c.json( res.isOk() ? { data: res.value, error: null } : { data: null, error: res.error }, res.isOk() ? 200 : 400, ); }) // Ensure account exists .put( "/ensure-account-exists", sValidator("json", ensureAccountExistsSchema), async (c) => { const fctx = c.env.locals.fCtx; const data = c.req.valid("json"); const res = await uc.ensureAccountExists(fctx, data.userId); return c.json( res.isOk() ? { data: res.value, error: null } : { data: null, error: res.error }, res.isOk() ? 200 : 400, ); }, ) // Check username availability .post( "/check-username", sValidator("json", checkUsernameSchema), async (c) => { const fctx = c.env.locals.fCtx; const data = c.req.valid("json"); const res = await uc.isUsernameAvailable(fctx, data.username); return c.json( res.isOk() ? { data: res.value, error: null } : { data: null, error: res.error }, res.isOk() ? 200 : 400, ); }, ) // Update last 2FA verification time .put("/update-2fa-verified/:userId", async (c) => { const fctx = c.env.locals.fCtx; const userId = c.req.param("userId"); const res = await uc.updateLastVerified2FaAtToNow(fctx, userId); return c.json( res.isOk() ? { data: res.value, error: null } : { data: null, error: res.error }, res.isOk() ? 200 : 400, ); }) // Ban user .post("/ban", sValidator("json", banUserSchema), async (c) => { const fctx = c.env.locals.fCtx; const data = c.req.valid("json"); const res = await uc.banUser(fctx, data.userId, data.reason, data.banExpiresAt); return c.json( res.isOk() ? { data: res.value, error: null } : { data: null, error: res.error }, res.isOk() ? 200 : 400, ); }) // Check if user is banned .get("/:userId/is-banned", async (c) => { const fctx = c.env.locals.fCtx; const userId = c.req.param("userId"); const res = await uc.isUserBanned(fctx, userId); return c.json( res.isOk() ? { data: res.value, error: null } : { data: null, error: res.error }, res.isOk() ? 200 : 400, ); }) // Get ban info .get("/:userId/ban-info", async (c) => { const fctx = c.env.locals.fCtx; const userId = c.req.param("userId"); const res = await uc.getBanInfo(fctx, userId); return c.json( res.isOk() ? { data: res.value, error: null } : { data: null, error: res.error }, res.isOk() ? 200 : 400, ); }) // Rotate password .put( "/rotate-password", sValidator("json", rotatePasswordSchema), async (c) => { const fctx = c.env.locals.fCtx; const data = c.req.valid("json"); const res = await uc.rotatePassword(fctx, data.userId, data.password); return c.json( res.isOk() ? { data: res.value, error: null } : { data: null, error: res.error }, res.isOk() ? 200 : 400, ); }, );