Language Changes
This commit is contained in:
50
lib/features/auth/controllers/auth_cubit.dart
Normal file
50
lib/features/auth/controllers/auth_cubit.dart
Normal file
@@ -0,0 +1,50 @@
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:kmobile/api/services/user_service.dart';
|
||||
import 'package:kmobile/core/errors/exceptions.dart';
|
||||
import '../../../data/repositories/auth_repository.dart';
|
||||
import 'auth_state.dart';
|
||||
|
||||
class AuthCubit extends Cubit<AuthState> {
|
||||
final AuthRepository _authRepository;
|
||||
final UserService _userService;
|
||||
|
||||
AuthCubit(this._authRepository, this._userService) : super(AuthInitial()) {
|
||||
checkAuthStatus();
|
||||
}
|
||||
|
||||
Future<void> checkAuthStatus() async {
|
||||
emit(AuthLoading());
|
||||
try {
|
||||
final isLoggedIn = await _authRepository.isLoggedIn();
|
||||
if (isLoggedIn) {
|
||||
final users = await _userService.getUserDetails();
|
||||
emit(Authenticated(users));
|
||||
} else {
|
||||
emit(Unauthenticated());
|
||||
}
|
||||
} catch (e) {
|
||||
emit(AuthError(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> refreshUserData() async {
|
||||
try {
|
||||
// emit(AuthLoading());
|
||||
final users = await _userService.getUserDetails();
|
||||
emit(Authenticated(users));
|
||||
} catch (e) {
|
||||
emit(AuthError('Failed to refresh user data: ${e.toString()}'));
|
||||
// Optionally, re-emit the previous state or handle as needed
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> login(String customerNo, String password) async {
|
||||
emit(AuthLoading());
|
||||
try {
|
||||
final users = await _authRepository.login(customerNo, password);
|
||||
emit(Authenticated(users));
|
||||
} catch (e) {
|
||||
emit(AuthError(e is AuthException ? e.message : e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
31
lib/features/auth/controllers/auth_state.dart
Normal file
31
lib/features/auth/controllers/auth_state.dart
Normal file
@@ -0,0 +1,31 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import '../../../data/models/user.dart';
|
||||
|
||||
abstract class AuthState extends Equatable {
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class AuthInitial extends AuthState {}
|
||||
|
||||
class AuthLoading extends AuthState {}
|
||||
|
||||
class Authenticated extends AuthState {
|
||||
final List<User> users;
|
||||
|
||||
Authenticated(this.users);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [users];
|
||||
}
|
||||
|
||||
class Unauthenticated extends AuthState {}
|
||||
|
||||
class AuthError extends AuthState {
|
||||
final String message;
|
||||
|
||||
AuthError(this.message);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [message];
|
||||
}
|
Reference in New Issue
Block a user