ditto/packages/ditto/middleware/requireSigner.ts
2025-02-16 23:27:13 -06:00

30 lines
918 B
TypeScript

import { MiddlewareHandler } from '@hono/hono';
import { HTTPException } from '@hono/hono/http-exception';
import { NostrSigner } from '@nostrify/nostrify';
import { SetRequired } from 'type-fest';
/** Throw a 401 if a signer isn't set. */
export const requireSigner: MiddlewareHandler<{ Variables: { user: { signer: NostrSigner } } }> = async (c, next) => {
if (!c.var.user) {
throw new HTTPException(401, { message: 'No pubkey provided' });
}
await next();
};
/** Throw a 401 if a NIP-44 signer isn't set. */
export const requireNip44Signer: MiddlewareHandler<
{ Variables: { user: { signer: SetRequired<NostrSigner, 'nip44'> } } }
> = async (c, next) => {
const { user } = c.var;
if (!user) {
throw new HTTPException(401, { message: 'No pubkey provided' });
}
if (!user.signer.nip44) {
throw new HTTPException(401, { message: 'No NIP-44 signer provided' });
}
await next();
};