diff options
| author | Adam Soltys <[email protected]> | 2025-09-04 08:45:01 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2025-09-04 08:45:01 -0700 |
| commit | e663c5f78dec8c788f822eb0765f9f8ec3a44277 (patch) | |
| tree | 72aa05e03ecfe262dea9946ba874a83995b81233 /lib | |
| parent | 0dc07d2e32a9afc5ae4728bb2c7c9b9b52059cdf (diff) | |
| download | coinos-server-e663c5f78dec8c788f822eb0765f9f8ec3a44277.tar.gz coinos-server-e663c5f78dec8c788f822eb0765f9f8ec3a44277.zip | |
support readonly tokens
Diffstat (limited to 'lib')
| -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); }, ); |
