Skip to content

Commit

Permalink
Revert "Install multiple_result and use Result in the FakeAuthReposit…
Browse files Browse the repository at this point in the history
…ory"

This reverts commit 17ef8476779e481a9ff15d533af3d3ddc31ebf64.
  • Loading branch information
bizz84 committed Dec 20, 2023
1 parent 5b59e64 commit e3b0997
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:ecommerce_app/src/features/authentication/domain/fake_app_user.d
import 'package:ecommerce_app/src/utils/delay.dart';
import 'package:ecommerce_app/src/utils/in_memory_store.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:multiple_result/multiple_result.dart';

class FakeAuthRepository {
FakeAuthRepository({this.addDelay = true});
Expand All @@ -17,40 +16,38 @@ class FakeAuthRepository {
// List to keep track of all user accounts
final List<FakeAppUser> _users = [];

Future<Result<void, AppException>> signInWithEmailAndPassword(
String email, String password) async {
Future<void> signInWithEmailAndPassword(String email, String password) async {
await delay(addDelay);
// check the given credentials agains each registered user
for (final u in _users) {
// matching email and password
if (u.email == email && u.password == password) {
_authState.value = u;
return const Success(null);
return;
}
// same email, wrong password
if (u.email == email && u.password != password) {
return Error(WrongPasswordException());
throw WrongPasswordException();
}
}
return Error(UserNotFoundException());
throw UserNotFoundException();
}

Future<Result<void, AppException>> createUserWithEmailAndPassword(
Future<void> createUserWithEmailAndPassword(
String email, String password) async {
await delay(addDelay);
// check if the email is already in use
for (final u in _users) {
if (u.email == email) {
return Error(EmailAlreadyInUseException());
throw EmailAlreadyInUseException();
}
}
// minimum password length requirement
if (password.length < 8) {
return Error(WeakPasswordException());
throw WeakPasswordException();
}
// create new user
_createNewUser(email, password);
return const Success(null);
}

Future<void> signOut() async {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'package:ecommerce_app/src/exceptions/app_exception.dart';
import 'package:ecommerce_app/src/features/authentication/data/fake_auth_repository.dart';
import 'package:ecommerce_app/src/features/authentication/presentation/sign_in/email_password_sign_in_state.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:multiple_result/multiple_result.dart';

class EmailPasswordSignInController
extends StateNotifier<EmailPasswordSignInState> {
Expand All @@ -14,17 +12,12 @@ class EmailPasswordSignInController

Future<bool> submit(String email, String password) async {
state = state.copyWith(value: const AsyncValue.loading());
final result = await _authenticate(email, password);
final value = result.when(
(success) => AsyncData(success),
(error) => AsyncError(error, StackTrace.current),
) as AsyncValue;
final value = await AsyncValue.guard(() => _authenticate(email, password));
state = state.copyWith(value: value);
return value.hasError == false;
}

Future<Result<void, AppException>> _authenticate(
String email, String password) {
Future<void> _authenticate(String email, String password) {
switch (state.formType) {
case EmailPasswordSignInFormType.signIn:
return authRepository.signInWithEmailAndPassword(email, password);
Expand Down
8 changes: 0 additions & 8 deletions ecommerce_app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.1"
multiple_result:
dependency: "direct main"
description:
name: multiple_result
sha256: "9aba99bd75f28ac6519980580636348c0327e225d950a239fa59d2d983af4ce4"
url: "https://pub.dev"
source: hosted
version: "5.0.0"
path:
dependency: transitive
description:
Expand Down
1 change: 0 additions & 1 deletion ecommerce_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ dependencies:
sembast: 3.5.0+1
sembast_web: 2.2.0
path_provider: 2.1.1
multiple_result: 5.0.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit e3b0997

Please sign in to comment.