@sigiljs-community/auth-plugin
Version:
Plugin for SigilJS framework that provides authentication with JWT-like tokens
78 lines (77 loc) • 2.38 kB
TypeScript
import { SigilPlugin } from '@sigiljs/sigil';
import { TokenPayload } from './web-tokens-controller';
export interface AuthPluginConfig {
/**
* Secret key for tokens generation
*
* While optional, it is strongly recommended to set up
* your own secret token for production environments
*
* @default Random 32 bytes long key
*/
secretKey?: Buffer | string;
/**
* List of protected routes
*
* If not set up, you will need to manually add modifier to each protected route
*/
protectedRoutes?: string[];
/**
* Define custom names for refresh and access token headers
*
* @default X-Sigil-Refresh-Token, Authorization
*/
authHeaders?: {
refreshToken: string;
accessToken: string;
};
}
/**
* Plugin for SigilJS framework that provides authentication with JWT-like tokens
*/
export default class AuthPlugin extends SigilPlugin<AuthPluginConfig> {
#private;
static name: string;
constructor();
onInitialize(): void;
/**
* Issue new access token with specified payload
*
* @param payload access token payload
* @param expiresIn
* @returns {string} generated access token
*/
issueAccessToken(payload: any, expiresIn?: number): string;
/**
* Issue new refresh token
*
* @returns {{refreshToken: string, refreshTokenHash: string}} generated refresh token
*/
issueRefreshToken(): {
refreshToken: string;
refreshTokenHash: string;
};
/**
* Check if specified access token is valid
*
* @param {string} token access token
* @param allowExpired if true, valid tokens will still valid even if expired
* @returns {boolean} is valid
*/
verifyAccessToken(token: string, allowExpired?: boolean): boolean;
/**
* Check refresh token integrity with stored hash
*
* @param {string} hash stored hash
* @param {string} token refresh token
* @returns {boolean} is valid
*/
verifyRefreshToken(hash: string, token: string): boolean;
/**
* Decode specified access token
*
* @param {string} token access token to decode
* @returns {TokenPayload | null} decode access token payload or null if in invalid format
*/
decodeWebToken<T = any>(token: string): TokenPayload<T> | null;
}