135 lines
4.1 KiB
Dart
135 lines
4.1 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 [],
|
|
});
|
|
|
|
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,
|
|
}) : this(
|
|
status: AuthStatus.error,
|
|
errorMessage: msg,
|
|
userId: userId,
|
|
role: role,
|
|
errorCode: errorCode,
|
|
);
|
|
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;
|
|
|
|
/// 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';
|
|
|
|
AuthState copyWith({
|
|
AuthStatus? status,
|
|
String? userId,
|
|
String? role,
|
|
String? errorMessage,
|
|
String? errorCode,
|
|
Map<String, dynamic>? totpData,
|
|
String? registerMessage,
|
|
bool? emailVerificationRequired,
|
|
List<String>? recoveryCodes,
|
|
bool clearError = false,
|
|
bool clearTotpData = false,
|
|
bool clearRecoveryCodes = 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),
|
|
);
|
|
}
|
|
|
|
@override
|
|
List<Object?> get props => [
|
|
status,
|
|
userId,
|
|
role,
|
|
errorMessage,
|
|
errorCode,
|
|
totpData,
|
|
registerMessage,
|
|
emailVerificationRequired,
|
|
recoveryCodes,
|
|
];
|
|
}
|