feat(logging): add logging to auth feature
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../../../../core/logging/logger.dart';
|
||||
import '../../data/repositories/auth_repository.dart';
|
||||
import 'auth_event.dart';
|
||||
import 'auth_state.dart';
|
||||
|
||||
class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
final AuthRepository _repository;
|
||||
final Logger _logger = getLogger('features.auth.bloc');
|
||||
|
||||
AuthBloc(this._repository) : super(AuthInitial()) {
|
||||
on<AuthStarted>(_onStarted);
|
||||
@@ -19,6 +21,10 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
final refreshToken = await _repository.getRefreshToken();
|
||||
if (refreshToken != null) {
|
||||
final response = await _repository.refreshSession(refreshToken);
|
||||
_logger.info(
|
||||
message: 'Session refreshed successfully',
|
||||
extra: {'user_id': response.user.id},
|
||||
);
|
||||
emit(
|
||||
AuthAuthenticated(
|
||||
user: AuthUser(id: response.user.id, phone: response.user.phone),
|
||||
@@ -29,11 +35,20 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
emit(
|
||||
const AuthUnauthenticated(reason: AuthUnauthenticatedReason.signedOut),
|
||||
);
|
||||
} catch (_) {
|
||||
} catch (e, stackTrace) {
|
||||
_logger.error(
|
||||
message: 'Session refresh failed',
|
||||
error: e,
|
||||
stackTrace: stackTrace,
|
||||
);
|
||||
try {
|
||||
await _repository.clearSessionLocalOnly();
|
||||
} catch (_) {
|
||||
// Keep state convergence even when storage cleanup fails.
|
||||
} catch (e, stackTrace) {
|
||||
_logger.error(
|
||||
message: 'Failed to clear local session',
|
||||
error: e,
|
||||
stackTrace: stackTrace,
|
||||
);
|
||||
} finally {
|
||||
emit(
|
||||
const AuthUnauthenticated(
|
||||
@@ -45,6 +60,7 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
}
|
||||
|
||||
void _onLoggedIn(AuthLoggedIn event, Emitter<AuthState> emit) {
|
||||
_logger.info(message: 'User logged in', extra: {'user_id': event.user.id});
|
||||
emit(AuthAuthenticated(user: event.user));
|
||||
}
|
||||
|
||||
@@ -54,8 +70,13 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
) async {
|
||||
try {
|
||||
await _repository.deleteSession();
|
||||
} catch (_) {
|
||||
// Keep state convergence even when logout cleanup fails.
|
||||
_logger.info(message: 'User logged out');
|
||||
} catch (e, stackTrace) {
|
||||
_logger.error(
|
||||
message: 'Failed to delete session on logout',
|
||||
error: e,
|
||||
stackTrace: stackTrace,
|
||||
);
|
||||
} finally {
|
||||
emit(
|
||||
const AuthUnauthenticated(reason: AuthUnauthenticatedReason.signedOut),
|
||||
@@ -67,10 +88,15 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
AuthSessionInvalidated event,
|
||||
Emitter<AuthState> emit,
|
||||
) async {
|
||||
_logger.warning(message: 'Session invalidated by server');
|
||||
try {
|
||||
await _repository.clearSessionLocalOnly();
|
||||
} catch (_) {
|
||||
// Keep state convergence even when local cleanup fails.
|
||||
} catch (e, stackTrace) {
|
||||
_logger.error(
|
||||
message: 'Failed to clear local session',
|
||||
error: e,
|
||||
stackTrace: stackTrace,
|
||||
);
|
||||
} finally {
|
||||
emit(
|
||||
const AuthUnauthenticated(reason: AuthUnauthenticatedReason.expired),
|
||||
|
||||
Reference in New Issue
Block a user