@analog-tools/auth
Version:
Authentication module for AnalogJS applications
89 lines (88 loc) • 2.15 kB
TypeScript
import { H3Event } from 'h3';
/**
* Represents an authentication route with a path and handler function
*/
export type AuthRoute = {
/**
* The path of the route relative to the auth base path
*/
path: string;
/**
* The handler function for the route that processes H3Event objects
*/
handler: (event: H3Event) => Promise<any> | any;
};
type StorageBasicConfig = {
ttl?: number;
prefix?: string;
sessionSecret?: string;
};
type RedisBasicConfig = {
tls?: boolean;
};
type RedisConnectionConfig = {
host: string;
port: number | string;
username?: string;
password?: string;
db?: number;
};
type RedisUrlConfig = {
url: string;
};
/**
* Redis session storage configuration
*/
export type RedisSessionConfig = StorageBasicConfig & RedisBasicConfig & (RedisUrlConfig | RedisConnectionConfig);
/**
* Memory session storage configuration
*/
export type MemorySessionConfig = StorageBasicConfig;
/**
* Cookie session storage configuration
*/
export type CookieSessionConfig = StorageBasicConfig & {
maxAge?: number;
secure?: boolean;
sameSite?: 'strict' | 'lax' | 'none';
domain?: string;
path?: string;
};
/**
* Type-safe session storage configuration using discriminated union
*/
export type SessionStorageConfig = {
type: 'redis';
config: RedisSessionConfig;
} | {
type: 'memory';
config: MemorySessionConfig;
} | {
type: 'cookie';
config: CookieSessionConfig;
};
export type UserHandler = {
mapUserToLocal?: <T>(user: T) => Promise<any> | any;
createOrUpdateUser?: <T>(user: T) => Promise<any>;
};
/**
* Configuration for analog auth
*/
export type AnalogAuthConfig = {
issuer: string;
clientId: string;
clientSecret: string;
audience?: string;
scope: string;
callbackUri: string;
tokenRefreshApiKey?: string;
unprotectedRoutes?: string[];
logoutUrl?: string;
/**
* Session storage configuration with type-safe mapping between
* storage type and corresponding configuration
*/
sessionStorage: SessionStorageConfig;
userHandler?: UserHandler;
};
export {};