Express.js
This is how your API handler would typically look like:
import express from "express";
import * as fs from "node:fs/promises";
import { bootstrapAuth, getUser } from "./auth"; // See below
function startExpressServer() {
bootstrapAuth({
implementation: "real", // or "mock", see: https://docs.oidc-spa.dev/v/v8/integration-guides/backend-token-validation/mock-modes
issuerUri: process.env.OIDC_ISSUER_URI!,
expectedAudience: process.env.OIDC_AUDIENCE
});
const app = express();
app.get("/api/todos", async (req, res) => {
const user = await getUser({ req, res });
const json = await fs.readFile(
`todos_${user.id}.json`,
"utf8"
);
res.status(200).type("application/json").send(json);
});
app.get("/api/todos-for-support/:userId", async (req, res) => {
// Will reject the request if user making the request
// doesn't have "support-staff" role
await getUser({ req, res, requiredRole: "support-staff" });
const json = await fs.readFile(
`todos_${req.params.userId}.json`,
"utf8"
);
res.status(200).type("application/json").send(json);
});
// ...
app.listen(parseInt(process.env.PORT ?? "3000"), () => {
console.log("Server running");
});
}Let's see how to export the utils to make it happen:
Last updated
Was this helpful?