99 lines
2.9 KiB
Dart
99 lines
2.9 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'auth_user.dart';
|
|
|
|
enum AuthStatus {
|
|
initial,
|
|
loading,
|
|
authenticated,
|
|
unauthenticated,
|
|
anonymous,
|
|
error,
|
|
}
|
|
|
|
/// Immutable authentication state representing the user session
|
|
/// across both online and offline/unconfigured environments.
|
|
@immutable
|
|
class AuthState {
|
|
final AuthStatus status;
|
|
final AuthUser? user;
|
|
final String? errorMessage;
|
|
final bool isFirebaseConfigured;
|
|
|
|
const AuthState({
|
|
this.status = AuthStatus.initial,
|
|
this.user,
|
|
this.errorMessage,
|
|
this.isFirebaseConfigured = true,
|
|
});
|
|
|
|
const AuthState.initial()
|
|
: status = AuthStatus.initial,
|
|
user = null,
|
|
errorMessage = null,
|
|
isFirebaseConfigured = true;
|
|
|
|
const AuthState.loading({this.user, this.isFirebaseConfigured = true})
|
|
: status = AuthStatus.loading,
|
|
errorMessage = null;
|
|
|
|
const AuthState.authenticated(this.user, {this.isFirebaseConfigured = true})
|
|
: status = AuthStatus.authenticated,
|
|
errorMessage = null;
|
|
|
|
const AuthState.anonymous(this.user, {this.isFirebaseConfigured = true})
|
|
: status = AuthStatus.anonymous,
|
|
errorMessage = null;
|
|
|
|
const AuthState.unauthenticated({this.isFirebaseConfigured = true})
|
|
: status = AuthStatus.unauthenticated,
|
|
user = null,
|
|
errorMessage = null;
|
|
|
|
const AuthState.error(String message, {this.user, this.isFirebaseConfigured = true})
|
|
: status = AuthStatus.error,
|
|
errorMessage = message;
|
|
|
|
/// Convenience getters for UI presentation
|
|
bool get isAuthenticated => status == AuthStatus.authenticated && user != null;
|
|
bool get isAnonymous => status == AuthStatus.anonymous || (user?.isAnonymous == true);
|
|
bool get isUnauthenticated => status == AuthStatus.unauthenticated || status == AuthStatus.initial;
|
|
bool get isLoading => status == AuthStatus.loading;
|
|
bool get hasError => status == AuthStatus.error && errorMessage != null;
|
|
|
|
AuthState copyWith({
|
|
AuthStatus? status,
|
|
AuthUser? user,
|
|
String? errorMessage,
|
|
bool? isFirebaseConfigured,
|
|
bool clearError = false,
|
|
}) {
|
|
return AuthState(
|
|
status: status ?? this.status,
|
|
user: user ?? this.user,
|
|
errorMessage: clearError ? null : (errorMessage ?? this.errorMessage),
|
|
isFirebaseConfigured: isFirebaseConfigured ?? this.isFirebaseConfigured,
|
|
);
|
|
}
|
|
|
|
@override
|
|
bool operator ==(Object other) =>
|
|
identical(this, other) ||
|
|
other is AuthState &&
|
|
runtimeType == other.runtimeType &&
|
|
status == other.status &&
|
|
user == other.user &&
|
|
errorMessage == other.errorMessage &&
|
|
isFirebaseConfigured == other.isFirebaseConfigured;
|
|
|
|
@override
|
|
int get hashCode =>
|
|
status.hashCode ^
|
|
user.hashCode ^
|
|
errorMessage.hashCode ^
|
|
isFirebaseConfigured.hashCode;
|
|
|
|
@override
|
|
String toString() =>
|
|
'AuthState(status: $status, user: $user, error: $errorMessage, configured: $isFirebaseConfigured)';
|
|
}
|