Files
cap/demo/main/server.js
T
2025-04-23 14:31:12 +01:00

55 lines
1.4 KiB
JavaScript

import fs from "fs";
import path from "path";
import Cap from "@cap.js/server";
import Fastify from "fastify";
const fastify = Fastify();
const cap = new Cap({
tokens_store_path: ".data/tokensList.json",
});
fastify.get("/", (req, res) => {
res.header("Content-Type", "text/html");
res.send(fs.createReadStream(path.join(__dirname, "index.html")));
});
fastify.get("/cap.js", (req, res) => {
res.header("Content-Type", "application/javascript");
res.send(
fs.createReadStream(path.join(__dirname, "../../widget/src/src/cap.js"))
);
});
fastify.get("/cap-floating.js", (req, res) => {
res.header("Content-Type", "application/javascript");
res.send(
fs.createReadStream(
path.join(__dirname, "../../widget/src/src/cap-floating.js")
)
);
});
fastify.post("/api/challenge", (req, res) => {
res.send(cap.createChallenge());
});
fastify.post("/api/redeem", async (req, res) => {
const { token, solutions } = req.body;
if (!token || !solutions) {
return res.code(400).send({ success: false });
}
const answer = await cap.redeemChallenge({ token, solutions });
res.send(answer);
console.log("new challenge redeemed", {
...answer,
isValid: (await cap.validateToken(answer.token)).success,
});
});
fastify.listen({ port: 3000, host: "0.0.0.0" }).then(() => {
console.log("Server is running on http://localhost:3000");
});