games_app/prediqo_mobile/lib/features/auth/bloc/auth_state.dart

147 lines
4.5 KiB
Dart

part of 'auth_bloc.dart';
/// High-level authentication lifecycle status emitted by [AuthBloc].
enum AuthStatus {
initial,
loading,
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.authenticated({
String? userId,
String? role,
List<String> 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<String, dynamic>? captcha,
}) : this(
status: AuthStatus.error,
errorMessage: msg,
userId: userId,
role: role,
errorCode: errorCode,
captcha: captcha,
);
const AuthState.totpSetup(Map<String, dynamic> 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<String, dynamic>? totpData;
final String? registerMessage;
final bool emailVerificationRequired;
/// One-time recovery codes shown after enabling / regenerating 2FA.
final List<String> recoveryCodes;
/// Captcha challenge from the API when login is challenged.
final Map<String, dynamic>? 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';
AuthState copyWith({
AuthStatus? status,
String? userId,
String? role,
String? errorMessage,
String? errorCode,
Map<String, dynamic>? totpData,
String? registerMessage,
bool? emailVerificationRequired,
List<String>? recoveryCodes,
Map<String, dynamic>? 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<Object?> get props => [
status,
userId,
role,
errorMessage,
errorCode,
totpData,
registerMessage,
emailVerificationRequired,
recoveryCodes,
captcha,
];
}