mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
30 lines
918 B
TypeScript
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();
|
|
};
|