aboutsummaryrefslogtreecommitdiffstats
path: root/lib/auth.ts
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-02-28 14:14:30 -0800
committerAdam Soltys <[email protected]>2026-02-28 14:14:30 -0800
commita1632ae402d11fdd7ffc360de97c55e0b4fad677 (patch)
treeaad5a1ea73f95dcd055c1d0b4ebf2708c36b4e01 /lib/auth.ts
parent76c7e52172f3dc25ee109434157d4d982ad9d2bd (diff)
downloadcoinos-server-a1632ae402d11fdd7ffc360de97c55e0b4fad677.tar.gz
coinos-server-a1632ae402d11fdd7ffc360de97c55e0b4fad677.zip
migrate to hono
Diffstat (limited to 'lib/auth.ts')
-rw-r--r--lib/auth.ts87
1 files changed, 41 insertions, 46 deletions
diff --git a/lib/auth.ts b/lib/auth.ts
index c2eca6d4..30d5438b 100644
--- a/lib/auth.ts
+++ b/lib/auth.ts
@@ -1,58 +1,53 @@
import config from "$config";
import { fail, getUser } from "$lib/utils";
-import fastifyPassport from "@fastify/passport";
-import jwt from "passport-jwt";
+import jwt from "jsonwebtoken";
+import { getCookie } from "hono/cookie";
-export const admin = {
- preValidation: fastifyPassport.authenticate(
- "jwt",
- { session: false },
- (req, res, err, user, info) => {
- if (!user.admin) return res.code(401).send("unauthorized");
- req.user = user;
- },
- ),
+const extractToken = (c) => {
+ const authHeader = c.req.header("authorization");
+ if (authHeader?.startsWith("Bearer ")) return authHeader.slice(7);
+ return getCookie(c, "token") || null;
};
-export const optional = {
- preValidation: fastifyPassport.authenticate(
- "jwt",
- { session: false },
- (req, res, err, user, info) => {
- req.user = user;
- },
- ),
-};
+const authenticate = async (c) => {
+ const token = extractToken(c);
+ if (!token) return null;
-export const auth = {
- preValidation: fastifyPassport.authenticate("jwt", {
- authInfo: false,
- session: false,
- }),
-};
+ try {
+ const payload = jwt.verify(token, config.jwt);
+ let { id } = payload as any;
+ const url = c.req.path;
+ const method = c.req.method;
-export const requirePin = async ({ body, user }) => {
- if (!user || (user.pin && user.pin !== body.pin)) fail("Invalid pin");
+ const wl = { GET: ["/invoice", "/payments"], POST: ["/invoice"] };
+ if (id.endsWith("-ro") && wl[method]?.some((p) => url.startsWith(p))) id = id.slice(0, -3);
+
+ return await getUser(id);
+ } catch {
+ return null;
+ }
};
-export const jwtStrategy = new jwt.Strategy(
- {
- jwtFromRequest: jwt.ExtractJwt.fromExtractors([
- jwt.ExtractJwt.fromAuthHeaderAsBearerToken(),
- (req) => (req.cookies ? req.cookies.token : null),
- ]),
- secretOrKey: config.jwt,
- passReqToCallback: true,
- },
- async (req, payload, next) => {
- const { originalUrl: u, method: m } = req;
+export const auth = async (c, next) => {
+ const user = await authenticate(c);
+ if (!user) return c.json("unauthorized", 401);
+ c.set("user", user);
+ await next();
+};
- let { id } = payload;
- const wl = { GET: ["/invoice", "/payments"], POST: ["/invoice"] };
+export const optional = async (c, next) => {
+ const user = await authenticate(c);
+ if (user) c.set("user", user);
+ await next();
+};
- if (id.endsWith("-ro") && wl[m].some((p) => u.startsWith(p))) id = id.slice(0, -3);
+export const admin = async (c, next) => {
+ const user = await authenticate(c);
+ if (!user?.admin) return c.json("unauthorized", 401);
+ c.set("user", user);
+ await next();
+};
- const user = await getUser(id);
- next(null, user);
- },
-);
+export const requirePin = async ({ body, user }) => {
+ if (!user || (user.pin && user.pin !== body.pin)) fail("Invalid pin");
+};