diff options
| author | Adam Soltys <[email protected]> | 2025-09-04 08:45:01 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2025-09-04 15:03:38 -0700 |
| commit | 044c1737289f89d111bcc8c3c709479286dcf29d (patch) | |
| tree | bec3ca898341a570a4ce32f85a9a16b5bb901ef5 /lib/auth.ts | |
| parent | b80ce59804e8fdc47fde12a0ff80d81cec59fb87 (diff) | |
| download | coinos-server-044c1737289f89d111bcc8c3c709479286dcf29d.tar.gz coinos-server-044c1737289f89d111bcc8c3c709479286dcf29d.zip | |
support readonly tokens
Diffstat (limited to 'lib/auth.ts')
| -rw-r--r-- | lib/auth.ts | 14 |
1 files changed, 11 insertions, 3 deletions
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); }, ); |
