Files
frislit/server/middlewares/auth.js
T
fristy 650807c1fb
CI / Docker image (push) Successful in 12s
CI / Server (push) Successful in 25s
CI / CLI (push) Successful in 13s
Development image / Development image (push) Successful in 21s
CI / CLI (windows) (push) Successful in 57s
CI / CLI (linux-arm64, static) (push) Successful in 1m37s
Release / Prepare release (push) Successful in 2s
Release / Server (linux-x64) (push) Successful in 53s
Release / CLI (linux-arm64, static) (push) Successful in 1m32s
Release / Docker image (push) Successful in 2m48s
Release / Server (windows-x64) (push) Successful in 6m20s
Release / CLI (windows-x64) (push) Successful in 6m48s
Release / CLI (linux-x64) (push) Successful in 7m8s
Release / Publish release (push) Successful in 1s
chore: restart the repository with a clean history
Squash the tree into one commit, drop docs/architecture.html, reset the version line to 0.3.0, consolidate the changelog, enable Renovate automerge for non-major updates and add AGENTS.md.
2026-11-10 11:31:13 +03:00

35 lines
1.4 KiB
JavaScript

const { tokenFromRequest } = require("../lib/sessions");
const { isSetupRequired } = require("../controllers/setup");
const deviceSession = async req => {
const device = await req.auth.deviceFor(req.auth.bearerFromHeaders(req.headers));
if (!device || device.guest) return null;
const account = await req.auth.accountFor(device);
return account && { account, username: account.username, role: account.role, device: true };
};
module.exports.authenticate = async (req, res, next) => {
const token = tokenFromRequest(req.raw);
const session = await req.sessions.get(token) || await deviceSession(req);
if (!session) return res.status(401).json({ code: 401, message: "You need to be logged in" });
req.session = session;
req.account = session.account;
req.sessionToken = token;
next();
};
module.exports.requireBrowser = (req, res, next) => {
if (req.session.device) return res.status(403).json({ code: 403, message: "Sign in on the website for that" });
next();
};
module.exports.requireAdmin = (req, res, next) => {
if (req.session.role !== "admin") return res.status(403).json({ code: 403, message: "Only an admin can do that" });
next();
};
module.exports.requireSetupComplete = async (req, res, next) => {
if (await isSetupRequired()) return res.status(409).json({ code: 409, message: "The server has not been set up yet" });
next();
};