@authxyz/local
Version:
A robust authentication library for express.js
65 lines (64 loc) • 3.71 kB
TypeScript
/// <reference types="cookie-parser" />
/// <reference types="qs" />
import type { SignOptions } from "jsonwebtoken";
import type { CookieOptions, NextFunction, Request, Response } from "express";
import type { Transporter } from "nodemailer";
import SMTPTransport from "nodemailer/lib/smtp-transport/index.js";
import { DatabaseAdapter } from "./types.js";
import MailResponder, { MailType } from "@authxyz/mail";
interface AuthType {
type: "JWT" | "COOKIE";
options: SignOptions | CookieOptions;
secret?: string;
verification: boolean;
}
export type PreProcessAuth = (req: Request, res: Response, next: NextFunction) => boolean;
export type PostProcessAuth<T extends Object> = (context: T, req: Request, res: Response) => void;
interface HandlerOptions<T, J> {
role: T;
body: (payload: any) => Object;
pre?: PreProcessAuth;
post?: PostProcessAuth<J>;
}
interface LocalAuthOption<T extends string> {
roles: T[];
adapter: DatabaseAdapter;
auth: AuthType;
mailClient?: Transporter<SMTPTransport.SentMessageInfo>;
}
type MailCallback<T extends Object> = (options: T) => MailResponder;
declare class Local<T extends string> {
#private;
constructor({ roles, adapter, auth, mailClient }: LocalAuthOption<T>);
protect(roles: T[]): (req: Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>, next: NextFunction) => any;
register(path: string, { role, body, pre, post, }: HandlerOptions<T, {
token: string;
email: string;
id: string;
}>): (req: Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>, next: NextFunction) => Promise<void>;
login(path: string, { role, body, pre, post }: HandlerOptions<T, {
user: Object;
}>): (req: Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>, next: NextFunction) => Promise<void>;
verify(path: string, { body, role, post, pre }: HandlerOptions<T, {
user: Object;
}>): (req: Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>, next: NextFunction) => Promise<void>;
resendVerification(path: string, { role, post, pre, }: HandlerOptions<T, {
user: Object;
verificationCode: Number;
}>): (req: Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>, next: NextFunction) => Promise<void>;
forgotPassword(path: string, { body, role, post, pre, }: HandlerOptions<T, {
user: Object;
resetCode: number;
}>): (req: Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>, next: NextFunction) => Promise<void>;
resetPassword(path: string, { body, role, post, pre, }: HandlerOptions<T, {
email: string;
user: Object;
}>): (req: Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>, next: NextFunction) => Promise<void>;
addTrigger(type: MailType, callback: MailCallback<{
verificationCode?: number;
user: Object;
}>): void;
private sendMail;
mailConfig(mailer: Transporter<SMTPTransport.SentMessageInfo>): void;
}
export default Local;