clonegur/src/server/decorators/auth.ts
2023-06-26 17:07:14 +03:00

15 lines
458 B
TypeScript

import { makeParameterModifier } from "../Router.ts";
export type AuthType = 'raw' | 'jwt';
export function auth(type: AuthType = 'jwt') {
return makeParameterModifier(req => {
let res = req.headers.authorization;
if (typeof res !== 'string') return undefined;
if (res.startsWith('Bearer')) res = res.substring(6).trimStart();
if (type === 'jwt') throw new Error('JWT is not supported.');
return res;
});
}