import { AuthFlowState, AuthFlowContext, StateDefinition, ResponseMetadata } from './auth-flow-state-machine.types'; import { AuthFlowContextBuilder } from './auth-flow-context-builder.service'; import { NAuthLogger } from '../utils/nauth-logger'; /** * Authentication Flow State Machine Service * * Core engine for evaluating authentication flow states using declarative rules. * Replaces imperative if/else logic with a rule-based state machine. * * **How it works:** * 1. Build context with pre-computed values * 2. Evaluate states in priority order (1-9) * 3. Select first state whose condition rule evaluates to true * 4. Execute onEnter hook if defined * 5. Return state with metadata * * **Benefits:** * - Declarative and maintainable * - Easy to test (pure functions) * - Extensible (add new states/rules easily) * - Clear priority ordering * * @example * ```typescript * const state = await stateMachine.evaluateState(context); * const definition = stateMachine.getStateDefinition(state); * ``` */ export declare class AuthFlowStateMachineService { private readonly contextBuilder; private readonly logger?; constructor(contextBuilder: AuthFlowContextBuilder, logger?: NAuthLogger | undefined); /** * Evaluate authentication flow state * * Evaluates states in priority order and returns the first matching state. * Executes onEnter hook if defined for the selected state. * * @param context - Authentication flow context * @returns Evaluated state * * @example * ```typescript * const context = await contextBuilder.build({ user, config, authMethod: 'password' }); * const state = await stateMachine.evaluateState(context); * // Returns: AuthFlowState.PENDING_EMAIL_VERIFICATION * ``` */ evaluateState(context: AuthFlowContext): Promise; /** * Get state definition by state * * @param state - State to get definition for * @returns State definition or undefined if not found * * @example * ```typescript * const def = stateMachine.getStateDefinition(AuthFlowState.PENDING_EMAIL_VERIFICATION); * ``` */ getStateDefinition(state: AuthFlowState): StateDefinition | undefined; /** * Build metadata for state response * * Calls buildMetadata function if defined for the state. * * @param state - State to build metadata for * @param context - Authentication flow context * @returns Metadata object or undefined * * @example * ```typescript * const metadata = await stateMachine.buildMetadata(state, context); * // Returns: { gracePeriodEndsAt: Date, riskScore: 45, riskLevel: 'medium' } * ``` */ buildMetadata(state: AuthFlowState, context: AuthFlowContext): ResponseMetadata | undefined; /** * Transition after challenge completion * * Re-evaluates state after a challenge is completed. * This is used in the challenge completion flow to determine the next state. * * @param params - Transition parameters * @param params.completedChallenge - Challenge that was just completed * @param params.context - Current authentication flow context * @param params.updateFn - Function to update user data (e.g., mark email as verified) * @returns New state after transition * * @example * ```typescript * const newState = await stateMachine.transitionAfterChallenge({ * completedChallenge: AuthChallenge.VERIFY_EMAIL, * context, * updateFn: async (user) => { * user.isEmailVerified = true; * await userRepository.save(user); * } * }); * ``` */ transitionAfterChallenge(params: { completedChallenge: string; context: AuthFlowContext; updateFn?: (user: AuthFlowContext['user']) => Promise; }): Promise; } //# sourceMappingURL=auth-flow-state-machine.service.d.ts.map