TNC Route Fixed
This commit is contained in:
@@ -42,49 +42,57 @@ class AuthCubit extends Cubit<AuthState> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> login(String customerNo, String password) async {
|
||||
emit(AuthLoading());
|
||||
try {
|
||||
final (users, authToken) = await _authRepository.login(customerNo, password);
|
||||
Future<void> login(String customerNo, String password) async {
|
||||
emit(AuthLoading());
|
||||
try {
|
||||
final (users, authToken) = await _authRepository.login(customerNo, password);
|
||||
|
||||
if (authToken.tnc == false) {
|
||||
// TNC not accepted, tell UI to show the dialog
|
||||
emit(ShowTncDialog(authToken, users));
|
||||
} else {
|
||||
// TNC already accepted, emit Authenticated and then proceed to MPIN check
|
||||
emit(Authenticated(users));
|
||||
await _checkMpinAndNavigate();
|
||||
}
|
||||
} catch (e) {
|
||||
emit(AuthError(e is AuthException ? e.message : e.toString()));
|
||||
}
|
||||
}
|
||||
if (authToken.tnc == false) {
|
||||
emit(ShowTncDialog(authToken, users));
|
||||
} else {
|
||||
await _checkMpinAndNavigate(users);
|
||||
}
|
||||
} catch (e) {
|
||||
emit(AuthError(e is AuthException ? e.message : e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Future<void> onTncDialogResult(
|
||||
bool agreed, AuthToken authToken, List<User> users) async {
|
||||
if (agreed) {
|
||||
try {
|
||||
await _authRepository.acceptTnc();
|
||||
// User agreed, emit Authenticated and then proceed to MPIN check
|
||||
emit(Authenticated(users));
|
||||
await _checkMpinAndNavigate();
|
||||
} catch (e) {
|
||||
emit(AuthError('Failed to accept TNC: $e'));
|
||||
}
|
||||
} else {
|
||||
// User disagreed, tell UI to navigate to the required screen
|
||||
emit(NavigateToTncRequiredScreen());
|
||||
}
|
||||
}
|
||||
Future<void> onTncDialogResult(
|
||||
bool agreed, AuthToken authToken, List<User> users) async {
|
||||
if (agreed) {
|
||||
try {
|
||||
await _authRepository.acceptTnc();
|
||||
// The user is NOT fully authenticated yet. Just check for MPIN.
|
||||
await _checkMpinAndNavigate(users);
|
||||
} catch (e) {
|
||||
emit(AuthError('Failed to accept TNC: $e'));
|
||||
}
|
||||
} else {
|
||||
emit(NavigateToTncRequiredScreen());
|
||||
}
|
||||
}
|
||||
|
||||
void mpinSetupCompleted() {
|
||||
if (state is NavigateToMpinSetupScreen) {
|
||||
final users = (state as NavigateToMpinSetupScreen).users;
|
||||
emit(Authenticated(users));
|
||||
} else {
|
||||
// Handle unexpected state if necessary
|
||||
emit(AuthError("Invalid state during MPIN setup completion."));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Future<void> _checkMpinAndNavigate(List<User> users) async {
|
||||
final mpin = await _secureStorage.read('mpin');
|
||||
if (mpin == null) {
|
||||
// No MPIN, tell UI to navigate to MPIN setup, carrying user data
|
||||
emit(NavigateToMpinSetupScreen(users));
|
||||
} else {
|
||||
// MPIN exists, user is authenticated
|
||||
emit(Authenticated(users));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _checkMpinAndNavigate() async {
|
||||
final mpin = await _secureStorage.read('mpin');
|
||||
if (mpin == null) {
|
||||
// No MPIN, tell UI to navigate to MPIN setup
|
||||
emit(NavigateToMpinSetupScreen());
|
||||
} else {
|
||||
// MPIN exists, tell UI to navigate to the dashboard
|
||||
emit(NavigateToDashboardScreen());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user