completely cleanup of the legacy hono router to leaner svelte remote functions
This commit is contained in:
@@ -1,170 +0,0 @@
|
||||
import {
|
||||
disable2FASchema,
|
||||
enable2FACodeSchema,
|
||||
startVerificationSchema,
|
||||
verifyCodeSchema,
|
||||
} from "./data";
|
||||
import { sValidator } from "@hono/standard-validator";
|
||||
import { HonoContext } from "@/core/hono.helpers";
|
||||
import { getTwofaController } from "./controller";
|
||||
import { auth } from "@domains/auth/config.base";
|
||||
import { Hono } from "hono";
|
||||
|
||||
const twofaController = getTwofaController();
|
||||
|
||||
export const twofaRouter = new Hono<HonoContext>()
|
||||
.post("/setup", async (c) => {
|
||||
const res = await twofaController.setup2FA(
|
||||
c.env.locals.fCtx,
|
||||
c.env.locals.user,
|
||||
);
|
||||
return c.json(
|
||||
res.isOk()
|
||||
? { data: res.value, error: null }
|
||||
: { data: null, error: res.error },
|
||||
res.isOk() ? 200 : 400,
|
||||
);
|
||||
})
|
||||
.post(
|
||||
"/verify-and-enable",
|
||||
sValidator("json", enable2FACodeSchema),
|
||||
async (c) => {
|
||||
const data = c.req.valid("json");
|
||||
const res = await twofaController.verifyAndEnable2FA(
|
||||
c.env.locals.fCtx,
|
||||
c.env.locals.user,
|
||||
data.code,
|
||||
c.req.raw.headers,
|
||||
);
|
||||
return c.json(
|
||||
res.isOk()
|
||||
? { data: res.value, error: null }
|
||||
: { data: null, error: res.error },
|
||||
res.isOk() ? 200 : 400,
|
||||
);
|
||||
},
|
||||
)
|
||||
.get("/generate-backup-codes", async (c) => {
|
||||
const res = await twofaController.generateBackupCodes(
|
||||
c.env.locals.fCtx,
|
||||
c.env.locals.user,
|
||||
);
|
||||
return c.json(
|
||||
res.isOk()
|
||||
? { data: res.value, error: null }
|
||||
: { data: null, error: res.error },
|
||||
res.isOk() ? 200 : 400,
|
||||
);
|
||||
})
|
||||
.delete("/disable", sValidator("json", disable2FASchema), async (c) => {
|
||||
const data = c.req.valid("json");
|
||||
const res = await twofaController.disable(
|
||||
c.env.locals.fCtx,
|
||||
c.env.locals.user,
|
||||
data.code,
|
||||
);
|
||||
return c.json(
|
||||
res.isOk()
|
||||
? { data: res.value, error: null }
|
||||
: { data: null, error: res.error },
|
||||
res.isOk() ? 200 : 400,
|
||||
);
|
||||
})
|
||||
.get("/requires-verification", async (c) => {
|
||||
const user = c.env.locals.user;
|
||||
const sessionId = c.req.query("sessionId")?.toString() ?? "";
|
||||
const res = await twofaController.requiresInitialVerification(
|
||||
c.env.locals.fCtx,
|
||||
user,
|
||||
sessionId,
|
||||
);
|
||||
return c.json(
|
||||
res.isOk()
|
||||
? { data: res.value, error: null }
|
||||
: { data: null, error: res.error },
|
||||
res.isOk() ? 200 : 400,
|
||||
);
|
||||
})
|
||||
.get("/requires-sensitive-action", async (c) => {
|
||||
const res = await twofaController.requiresSensitiveActionVerification(
|
||||
c.env.locals.fCtx,
|
||||
c.env.locals.user,
|
||||
);
|
||||
return c.json(
|
||||
res.isOk()
|
||||
? { data: res.value, error: null }
|
||||
: { data: null, error: res.error },
|
||||
res.isOk() ? 200 : 400,
|
||||
);
|
||||
})
|
||||
.post(
|
||||
"/start-verification-session",
|
||||
sValidator("json", startVerificationSchema),
|
||||
async (c) => {
|
||||
const data = c.req.valid("json");
|
||||
|
||||
const ipAddress =
|
||||
c.req.header("x-forwarded-for") ||
|
||||
c.req.header("x-real-ip") ||
|
||||
"unknown";
|
||||
const userAgent = c.req.header("user-agent") || "unknown";
|
||||
|
||||
const res = await twofaController.startVerification(
|
||||
c.env.locals.fCtx,
|
||||
{
|
||||
userId: data.userId,
|
||||
sessionId: data.sessionId,
|
||||
ipAddress,
|
||||
userAgent,
|
||||
},
|
||||
);
|
||||
|
||||
return c.json(
|
||||
res.isOk()
|
||||
? { data: res.value, error: null }
|
||||
: { data: null, error: res.error },
|
||||
);
|
||||
},
|
||||
)
|
||||
.post(
|
||||
"/verify-session-code",
|
||||
sValidator("json", verifyCodeSchema),
|
||||
async (c) => {
|
||||
const data = c.req.valid("json");
|
||||
|
||||
let user = c.env.locals.user;
|
||||
if (!user) {
|
||||
const out = await auth.api.getSession({
|
||||
headers: c.req.raw.headers,
|
||||
});
|
||||
user = out?.user as any;
|
||||
}
|
||||
|
||||
const res = await twofaController.verifyCode(
|
||||
c.env.locals.fCtx,
|
||||
{
|
||||
verificationSessToken: data.verificationToken,
|
||||
code: data.code,
|
||||
},
|
||||
user,
|
||||
);
|
||||
|
||||
return c.json(
|
||||
res.isOk()
|
||||
? { data: res.value, error: null }
|
||||
: { data: null, error: res.error },
|
||||
res.isOk() ? 200 : 400,
|
||||
);
|
||||
},
|
||||
)
|
||||
.post("/cleanup-expired-sessions", async (c) => {
|
||||
const res = await twofaController.cleanupExpiredSessions(
|
||||
c.env.locals.fCtx,
|
||||
);
|
||||
return c.json(
|
||||
res.isOk()
|
||||
? { data: res.value, error: null }
|
||||
: { data: null, error: res.error },
|
||||
res.isOk() ? 200 : 400,
|
||||
);
|
||||
});
|
||||
@@ -1,165 +0,0 @@
|
||||
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<HonoContext>()
|
||||
// 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,
|
||||
);
|
||||
},
|
||||
);
|
||||
Reference in New Issue
Block a user