Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mock auth #231

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions app/lib/app/data/data.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
export 'repository/app_repositoty_impl.dart';
export 'repository/theme_repository_impl.dart';
export 'repository/auth_repositoty_impl.dart';
export 'source/app_local_data_source.dart';
export 'source/theme_local_data_source.dart';
export 'source/auth_local_data_source.dart';
export 'source/auth_remote_data_source.dart';
export 'source/local/app_local_data_source.dart';
export 'source/local/theme_local_data_source.dart';
export 'source/local/auth_local_data_source.dart';
export 'source/remote/auth_remote_data_source_impl.dart';
export 'source/remote/auth_remote_data_source.dart';
export 'source/remote/auth_mock_remote_data_source.dart';
export 'models/user_model.dart';
export 'models/token_response.dart';
export 'models/user_data_response.dart';
47 changes: 43 additions & 4 deletions app/lib/app/data/repository/auth_repositoty_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,44 @@ final class AuthRepositoryImpl implements AuthRepository {
}
}

@override
Future<void> loginWithEmail(String email) async {
try {
await remoteDataSource.loginWithEmail(email);
} catch (e, s) {
MqCrashlytics.report(e, s);
log('signWithEmail: error: $e\n$s');
}
}

@override
Future<Either<UserEntity, Exception>> fetchSmsCode({
required String code,
required String languageCode,
required Gender gender,
}) async {
try {
final res = await remoteDataSource.fetchSmsCode(code: code, languageCode: languageCode, gender: gender);

return res.fold(
Left.new,
(r) {
final userEntity = UserEntity(
accessToken: r.accessToken,
username: r.username,
gender: r.gender,
localeCode: r.localeCode,
);
return Right(userEntity);
},
);
} catch (e, s) {
log('signWithemail: error: $e\n$s');
MqCrashlytics.report(e, s);
return Left(AuthenticationExc(message: e.toString()));
}
}

@override
Future<Either<UserEntity, Exception>> signWithGoogle(
String languageCode,
Expand All @@ -39,14 +77,15 @@ final class AuthRepositoryImpl implements AuthRepository {
final res = await remoteDataSource.signInWithGoogle(languageCode, gender);
return res.fold(
Left.new,
(r) => Right(
UserEntity(
(r) {
final userEntity = UserEntity(
accessToken: r.accessToken,
username: r.username,
gender: r.gender,
localeCode: r.localeCode,
),
),
);
return Right(userEntity);
},
);
} catch (e, s) {
log('signWithGoogle: error: $e\n$s');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class AuthLocalDataSource {
final userGender = storage.readString(key: StorageKeys.genderKey);
final username = storage.readString(key: StorageKeys.usernameKey);
final localeCode = storage.readString(key: StorageKeys.localeKey);

if (userToken == null && userGender == null && username == null) return null;
return UserEntity(
accessToken: userToken!,
Expand Down
101 changes: 101 additions & 0 deletions app/lib/app/data/source/remote/auth_mock_remote_data_source.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import 'package:meta/meta.dart';
import 'package:mq_storage/mq_storage.dart';
import 'package:my_quran/app/app.dart';
import 'package:my_quran/constants/contants.dart';

import 'package:my_quran/core/core.dart';

@immutable
final class AuthMockRemoteDataSource implements AuthRemoteDataSource {
const AuthMockRemoteDataSource(this.storage);
final PreferencesStorage storage;

@override
Future<void> loginWithEmail(String email) async {
return;
}

@override
Future<Either<UserModelResponse, Exception>> fetchSmsCode({
required String code,
required String languageCode,
required Gender gender,
}) async {
final user = UserModelResponse(
accessToken: r'myquran_te$t_t0ken',
username: 'Test User',
gender: gender,
localeCode: languageCode,
);
await storage.writeString(key: StorageKeys.tokenKey, value: user.accessToken);
await storage.writeString(key: StorageKeys.usernameKey, value: user.username);

return Right(user);
}

@override
Future<Either<UserModelResponse, Exception>> signInWithGoogle(
String languageCode,
Gender gender,
) async {
final user = UserModelResponse(
accessToken: 'mock_token_google',
username: 'Mock Google User',
gender: gender,
localeCode: languageCode,
);
return Right(user);
}

@override
Future<Either<UserModelResponse, Exception>> signInWithApple(
String languageCode,
Gender gender,
) async {
final user = UserModelResponse(
accessToken: 'mock_token_apple',
username: 'Mock Apple User',
gender: gender,
localeCode: languageCode,
);
return Right(user);
}

@override
Future<Either<UserDataResponse, Exception>> saveUserData(UserEntity userEntity) async {
final response = UserDataResponse(
gender: userEntity.gender.toString(),
language: userEntity.localeCode,
);
return Right(response);
}

@override
Future<Either<UserDataResponse, Exception>> pathGender({
required String userId,
required Gender gender,
}) async {
final response = UserDataResponse(
gender: Gender.male.toString(),
language: 'en',
);
return Right(response);
}

@override
Future<Either<UserDataResponse, Exception>> pathLocaleCode({
required String userId,
required String localeCode,
}) async {
final response = UserDataResponse(
gender: Gender.male.toString(),
language: 'en',
);
return Right(response);
}

@override
Future<void> logoutRemote() async {
return;
}
}
36 changes: 36 additions & 0 deletions app/lib/app/data/source/remote/auth_remote_data_source.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:my_quran/app/app.dart';
import 'package:my_quran/core/core.dart';

abstract class AuthRemoteDataSource {
Future<void> loginWithEmail(String email);

Future<Either<UserModelResponse, Exception>> fetchSmsCode({
required String code,
required String languageCode,
required Gender gender,
});

Future<Either<UserModelResponse, Exception>> signInWithGoogle(
String languageCode,
Gender gender,
);

Future<Either<UserModelResponse, Exception>> signInWithApple(
String languageCode,
Gender gender,
);

Future<Either<UserDataResponse, Exception>> saveUserData(UserEntity userEntity);

Future<Either<UserDataResponse, Exception>> pathGender({
required String userId,
required Gender gender,
});

Future<Either<UserDataResponse, Exception>> pathLocaleCode({
required String userId,
required String localeCode,
});

Future<void> logoutRemote();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import 'package:my_quran/constants/contants.dart';
import 'package:my_quran/core/core.dart';

@immutable
final class AuthRemoteDataSource {
const AuthRemoteDataSource({
final class AuthRemoteDataSourceImpl implements AuthRemoteDataSource {
const AuthRemoteDataSourceImpl({
required this.client,
required this.storage,
required this.soccialAuth,
Expand All @@ -19,6 +19,54 @@ final class AuthRemoteDataSource {
final SoccialAuth soccialAuth;
final bool isIntegrationTest;

@override
Future<void> loginWithEmail(String email) async {
try {
await client.post(
apiConst.loginWithEmail,
fromJson: TokenResponse.fromJson,
body: {'email': email},
);
} catch (e) {
throw Exception('Error during login: $e');
}
}

@override
Future<Either<UserModelResponse, Exception>> fetchSmsCode({
required String code,
required String languageCode,
required Gender gender,
}) async {
try {
final token = await client.post(
apiConst.fetchSmsCode,
fromJson: TokenResponse.fromJson,
body: {'code': code},
);
return token.fold(
(error) {
return Left(error);
},
(r) async {
final user = UserModelResponse(
accessToken: r.key,
username: '',
gender: gender,
localeCode: languageCode,
);

await storage.writeString(key: StorageKeys.tokenKey, value: user.accessToken);

return Right(user);
},
);
} catch (e) {
return Left(Exception('Error fetching SMS code: $e'));
}
}

@override
Future<Either<UserModelResponse, Exception>> signInWithGoogle(
String languageCode,
Gender gender,
Expand Down Expand Up @@ -62,6 +110,7 @@ final class AuthRemoteDataSource {
}
}

@override
Future<Either<UserModelResponse, Exception>> signInWithApple(
String languageCode,
Gender gender,
Expand Down Expand Up @@ -105,6 +154,7 @@ final class AuthRemoteDataSource {
}
}

@override
Future<Either<UserDataResponse, Exception>> saveUserData(UserEntity userEntity) {
return client.putType(
apiConst.putProfile(userEntity.accessToken),
Expand All @@ -116,6 +166,7 @@ final class AuthRemoteDataSource {
);
}

@override
Future<Either<UserDataResponse, Exception>> pathGender({
required String userId,
required Gender gender,
Expand All @@ -127,6 +178,7 @@ final class AuthRemoteDataSource {
);
}

@override
Future<Either<UserDataResponse, Exception>> pathLocaleCode({
required String userId,
required String localeCode,
Expand All @@ -138,6 +190,7 @@ final class AuthRemoteDataSource {
);
}

@override
Future<void> logoutRemote() async {
await soccialAuth.logOut();
}
Expand Down
6 changes: 6 additions & 0 deletions app/lib/app/data/source/source.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export 'local/app_local_data_source.dart';
export 'local/auth_local_data_source.dart';
export 'local/theme_local_data_source.dart';
export 'remote/auth_mock_remote_data_source.dart';
export 'remote/auth_remote_data_source.dart';
export 'remote/auth_remote_data_source_impl.dart';
2 changes: 2 additions & 0 deletions app/lib/app/domain/domain.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ export 'usecase/ser_user_data_user_case.dart';
export 'usecase/patch_gender_user_case.dart';
export 'usecase/patch_locale_code_use_case.dart';
export 'usecase/logout_use_case.dart';
export 'usecase/email_login_usecase.dart';
export 'usecase/fetch_sms_code_use_case.dart';
export 'entity/user_entity.dart';
export 'entity/user_data_entity.dart';
7 changes: 7 additions & 0 deletions app/lib/app/domain/repository/auth_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import 'package:my_quran/core/core.dart';
abstract class AuthRepository {
UserEntity? get init;

Future<void> loginWithEmail(String email);

Future<Either<UserEntity, Exception>> fetchSmsCode({
required String code,
required String languageCode,
required Gender gender,
});
Future<Either<UserEntity, Exception>> signWithGoogle(
String languageCode,
Gender gender,
Expand Down
13 changes: 13 additions & 0 deletions app/lib/app/domain/usecase/email_login_usecase.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:meta/meta.dart';
import 'package:my_quran/app/app.dart';

@immutable
final class EmailLoginUseCase {
const EmailLoginUseCase(this.repository);

final AuthRepository repository;

Future<void> call(String email) {
return repository.loginWithEmail(email);
}
}
Loading
Loading