UNPKG

nest-authify

Version:

Complete authentication and authorization package for NestJS - Monolith and Microservices ready with OAuth, JWT, Redis sessions

133 lines (132 loc) 3.48 kB
import { ModuleMetadata, Type } from '@nestjs/common'; import { IAuthService } from '../core/interfaces/auth-service.interface'; import { IAuthRepository } from './auth-repository.interface'; import { ISessionStore } from './session-store.interface'; export type AuthMode = 'normal' | 'server' | 'client'; export interface RedisConfig { host: string; port: number; password?: string; db?: number; keyPrefix?: string; ttl?: number; } export interface SessionStoreConfig { type: 'memory' | 'redis'; redis?: RedisConfig; } export interface GoogleOAuthConfig { clientId: string; clientSecret: string; callbackUrl?: string; scope?: string[]; } export interface FacebookOAuthConfig { clientId: string; clientSecret: string; callbackUrl?: string; scope?: string[]; profileFields?: string[]; } export interface GithubOAuthConfig { clientId: string; clientSecret: string; callbackUrl?: string; scope?: string[]; } export interface AuthStrategies { local?: boolean; jwt?: boolean; google?: boolean; facebook?: boolean; github?: boolean; } export type HashCallback = (password: string) => Promise<string>; export type HashVerifyCallback = (password: string, hash: string) => Promise<boolean>; export interface AuthModuleOptions { mode: AuthMode; jwtSecret: string; jwtExpiresIn?: string; refreshExpiresIn?: string; sessionStore?: SessionStoreConfig | Type<ISessionStore>; authService?: Type<IAuthService>; authRepository?: Type<IAuthRepository>; hashCallback?: HashCallback; hashVerifyCallback?: HashVerifyCallback; google?: GoogleOAuthConfig; facebook?: FacebookOAuthConfig; github?: GithubOAuthConfig; strategies?: AuthStrategies; microserviceOptions?: { transport?: any; options?: any; }; enableControllers?: boolean; controllersPrefix?: string; enableSwagger?: boolean; } export interface AuthModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> { useFactory: (...args: any[]) => Promise<AuthModuleOptions> | AuthModuleOptions; inject?: any[]; } export interface JwtPayload { sub: string; username?: string; email?: string; roles?: string[]; permissions?: string[]; sessionId?: string; iat?: number; exp?: number; } export interface AuthSession { accessToken: string; refreshToken: string; expiresIn: number; tokenType: string; sub: string; sessionId: string; [key: string]: any; } export interface AuthUser { id: string; username?: string; email?: string; password?: string; roles?: string[]; permissions?: string[]; isActive?: boolean; emailVerified?: boolean; provider?: string; providerId?: string; [key: string]: any; } export interface ValidatedUser { id: string; username?: string; email?: string; roles?: string[]; permissions?: string[]; [key: string]: any; } export interface RegisterUserDto { username?: string; email: string; password: string; [key: string]: any; } export interface LoginDto { username?: string; email?: string; password: string; } export interface LoginResponse extends AuthSession { user: Partial<AuthUser>; } export interface CreateSessionOptions { provider?: string; providerId?: string; expiresIn?: string; refreshExpiresIn?: string; metadata?: Record<string, any>; }