part of 'auth_bloc.dart'; /// High-level authentication lifecycle status emitted by [AuthBloc]. enum AuthStatus { initial, /// Session restore / bootstrap (splash, router). loading, /// Form submit in flight (login / register / resend) — keeps guest UI mounted. submitting, authenticated, unauthenticated, registered, error, totpSetup, totpEnabled, verificationResent, } /// Immutable authentication state including user identity and 2FA setup payload. class AuthState extends Equatable { const AuthState({ this.status = AuthStatus.initial, this.userId, this.role, this.errorMessage, this.errorCode, this.totpData, this.registerMessage, this.emailVerificationRequired = false, this.recoveryCodes = const [], this.captcha, }); const AuthState.initial() : this(); const AuthState.loading() : this(status: AuthStatus.loading); const AuthState.submitting() : this(status: AuthStatus.submitting); const AuthState.authenticated({ String? userId, String? role, List recoveryCodes = const [], }) : this( status: AuthStatus.authenticated, userId: userId, role: role, recoveryCodes: recoveryCodes, ); const AuthState.unauthenticated() : this(status: AuthStatus.unauthenticated); const AuthState.registered({ String? message, bool emailVerificationRequired = true, }) : this( status: AuthStatus.registered, registerMessage: message, emailVerificationRequired: emailVerificationRequired, ); const AuthState.verificationResent({String? message}) : this(status: AuthStatus.verificationResent, registerMessage: message); const AuthState.totpEnabled({String? userId, String? role}) : this(status: AuthStatus.totpEnabled, userId: userId, role: role); const AuthState.error( String msg, { String? userId, String? role, String? errorCode, Map? captcha, }) : this( status: AuthStatus.error, errorMessage: msg, userId: userId, role: role, errorCode: errorCode, captcha: captcha, ); const AuthState.totpSetup(Map data, {String? userId, String? role}) : this(status: AuthStatus.totpSetup, totpData: data, userId: userId, role: role); final AuthStatus status; final String? userId; final String? role; final String? errorMessage; final String? errorCode; final Map? totpData; final String? registerMessage; final bool emailVerificationRequired; /// One-time recovery codes shown after enabling / regenerating 2FA. final List recoveryCodes; /// Captcha challenge from the API when login is challenged. final Map? captcha; /// Whether the signed-in user has administrator access (admin or super_admin). bool get isAdmin => role == 'admin' || role == 'super_admin'; /// True while a session should stay active (including in-progress 2FA flows). bool get isSignedIn => status == AuthStatus.authenticated || status == AuthStatus.totpSetup || status == AuthStatus.totpEnabled || (status == AuthStatus.error && userId != null); bool get isEmailNotVerified => errorCode == 'EMAIL_NOT_VERIFIED'; bool get isCaptchaChallenge => errorCode == 'CAPTCHA_REQUIRED' || errorCode == 'CAPTCHA_INVALID'; /// True while a guest auth form submit is in flight. bool get isSubmitting => status == AuthStatus.submitting; AuthState copyWith({ AuthStatus? status, String? userId, String? role, String? errorMessage, String? errorCode, Map? totpData, String? registerMessage, bool? emailVerificationRequired, List? recoveryCodes, Map? captcha, bool clearError = false, bool clearTotpData = false, bool clearRecoveryCodes = false, bool clearCaptcha = false, }) { return AuthState( status: status ?? this.status, userId: userId ?? this.userId, role: role ?? this.role, errorMessage: clearError ? null : (errorMessage ?? this.errorMessage), errorCode: clearError ? null : (errorCode ?? this.errorCode), totpData: clearTotpData ? null : (totpData ?? this.totpData), registerMessage: registerMessage ?? this.registerMessage, emailVerificationRequired: emailVerificationRequired ?? this.emailVerificationRequired, recoveryCodes: clearRecoveryCodes ? const [] : (recoveryCodes ?? this.recoveryCodes), captcha: clearCaptcha || clearError ? null : (captcha ?? this.captcha), ); } @override List get props => [ status, userId, role, errorMessage, errorCode, totpData, registerMessage, emailVerificationRequired, recoveryCodes, captcha, ]; }