@foal/jwt
Version:
Authentication with JWT for FoalTS
50 lines (49 loc) • 1.48 kB
TypeScript
import { Context, HookDecorator, ServiceManager } from '@foal/core';
/**
* Options of the hooks created by JWTRequired and JWTOptional.
*
* @export
* @interface JWTOptions
*/
export type JWTOptions = {
secretOrPublicKey?: (header: any, payload: any) => Promise<string>;
blackList?: (token: string) => boolean | Promise<boolean>;
cookie?: boolean;
csrf?: boolean;
/**
* Add openapi metadata to the class or class method.
*
* @type {boolean}
* @memberof JWTOptions
*/
openapi?: boolean;
} & ({
userIdType: 'string';
user?: (id: string, services: ServiceManager) => Promise<Context['user']>;
} | {
userIdType?: 'number';
user?: (id: number, services: ServiceManager) => Promise<Context['user']>;
});
export interface VerifyOptions {
algorithms?: string[];
audience?: string | RegExp | (string | RegExp)[];
complete?: boolean;
issuer?: string | string[];
ignoreExpiration?: boolean;
ignoreNotBefore?: boolean;
subject?: string;
clockTolerance?: number;
maxAge?: string | number;
clockTimestamp?: number;
nonce?: string;
}
/**
* Sub-function used by JWTRequired and JWTOptional to avoid code duplication.
*
* @export
* @param {boolean} required
* @param {JWTOptions} options
* @param {VerifyOptions} verifyOptions
* @returns {HookDecorator}
*/
export declare function JWT(required: boolean, options: JWTOptions, verifyOptions: VerifyOptions): HookDecorator;