33 lines
1.0 KiB
TypeScript
33 lines
1.0 KiB
TypeScript
import { Router } from "@oak/oak";
|
|
import { APIErrorCode, APIException } from "../model/interfaces.ts";
|
|
import { type AuthContext, authMiddleware } from "../middleware/auth.ts";
|
|
import { createInvite, validateInvite } from "../services/invite-service.ts";
|
|
|
|
const router = new Router({ prefix: "/api/invites" });
|
|
|
|
// Create a new invite link (any authenticated user)
|
|
router.post("/", authMiddleware, async (ctx: AuthContext) => {
|
|
if (!ctx.state.user) {
|
|
throw new APIException(APIErrorCode.UNAUTHORIZED, 401, "Not authenticated");
|
|
}
|
|
const token = await createInvite(ctx.state.user.userId);
|
|
ctx.response.status = 201;
|
|
ctx.response.body = { success: true, data: { token } };
|
|
});
|
|
|
|
// Validate an invite token (used by the register page before showing the form)
|
|
router.get("/:token", async (ctx) => {
|
|
try {
|
|
await validateInvite(ctx.params.token);
|
|
ctx.response.body = { success: true, data: null };
|
|
} catch {
|
|
throw new APIException(
|
|
APIErrorCode.NOT_FOUND,
|
|
404,
|
|
"Invalid or expired invite",
|
|
);
|
|
}
|
|
});
|
|
|
|
export default router;
|