import express from "express";
import bodyParser from "body-parser";
import crypto from "crypto";
const app = express();
app.use(bodyParser.json());
// In-memory stores
const users = {}; // { userId: username }
const sessions = {}; // { token: userId }
// Basic HTML UI
const HTML_PAGE = `
Fake Auth API (Express)
Fake Auth API
Users
`;
// === Routes ===
// UI page
app.get("/", (req, res) => res.send(HTML_PAGE));
// Get users
app.get("/api/users", (req, res) => res.json(users));
// Add user
app.post("/api/users", (req, res) => {
const { id, name } = req.body;
if (!id || !name) return res.status(400).json({ error: "Missing id or name" });
users[id] = name;
res.json({ status: "ok" });
});
// Create token
app.post("/api/create_token/:uid", (req, res) => {
const uid = req.params.uid;
if (!users[uid]) return res.status(404).json({ error: "User not found" });
const token = crypto.randomBytes(8).toString("hex");
sessions[token] = uid;
res.json({ token });
});
// Verify token
app.get("/api/verify/:token", (req, res) => {
const uid = sessions[req.params.token];
if (!uid) return res.status(404).json({ error: "Invalid token" });
res.json({ id: uid, name: users[uid] });
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`[FakeAuth] Running on http://localhost:${PORT}`));