From 044c1737289f89d111bcc8c3c709479286dcf29d Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 4 Sep 2025 08:45:01 -0700 Subject: support readonly tokens --- lib/auth.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/auth.ts b/lib/auth.ts index 0dfff4ec..79c04e9d 100644 --- a/lib/auth.ts +++ b/lib/auth.ts @@ -1,5 +1,4 @@ import config from "$config"; -import { g } from "$lib/db"; import { fail, getUser } from "$lib/utils"; import fastifyPassport from "@fastify/passport"; import jwt from "passport-jwt"; @@ -43,9 +42,18 @@ export const jwtStrategy = new jwt.Strategy( (req) => (req.cookies ? req.cookies.token : null), ]), secretOrKey: config.jwt, + passReqToCallback: true, }, - async (payload, next) => { - const user = await getUser(payload.id); + async (req, payload, next) => { + const { originalUrl: u, method: m } = req; + + let { id } = payload; + const wl = { GET: ["/invoice", "/payments"], POST: ["/invoice"] }; + + if (id.endsWith("-ro") && wl[m].some((p) => u.startsWith(p))) + id = id.slice(0, -3); + + const user = await getUser(id); next(null, user); }, ); -- cgit v1.2.3