Hey is a decentralized and permissionless social media app built with Lens Protocol 馃尶
at main 32 lines 810 B view raw
1import { LENS_API_URL } from "@hey/data/constants"; 2import { withPrefix } from "@hey/helpers/logger"; 3import type { Context, Next } from "hono"; 4import { createRemoteJWKSet, jwtVerify } from "jose"; 5 6const jwksUri = `${LENS_API_URL.replace("/graphql", "")}/.well-known/jwks.json`; 7const JWKS = createRemoteJWKSet(new URL(jwksUri), { 8 cacheMaxAge: 60 * 60 * 12 9}); 10 11const unauthorized = (c: Context) => c.body("Unauthorized", 401); 12 13const authMiddleware = async (c: Context, next: Next) => { 14 const log = withPrefix("[API]"); 15 const token = c.get("token"); 16 17 if (!token) { 18 log.warn("missing token"); 19 return unauthorized(c); 20 } 21 22 try { 23 await jwtVerify(token, JWKS); 24 } catch { 25 log.warn("invalid token"); 26 return unauthorized(c); 27 } 28 29 return next(); 30}; 31 32export default authMiddleware;