diff --git a/packages/wyatt_authentication_bloc/example/lib/core/dependency_injection/get_it.dart b/packages/wyatt_authentication_bloc/example/lib/core/dependency_injection/get_it.dart index 29cd0a73..d1cb33ab 100644 --- a/packages/wyatt_authentication_bloc/example/lib/core/dependency_injection/get_it.dart +++ b/packages/wyatt_authentication_bloc/example/lib/core/dependency_injection/get_it.dart @@ -50,8 +50,8 @@ abstract class GetItInitializer { ), ]) : () => AuthenticationFirebaseDataSourceImpl( - googleSignIn: GoogleSignIn(clientId: DefaultFirebaseOptions.ios.iosClientId) - ), + googleSignIn: GoogleSignIn( + clientId: DefaultFirebaseOptions.ios.iosClientId)), ) ..registerLazySingleton>( () => AuthenticationCacheDataSourceImpl(), diff --git a/packages/wyatt_authentication_bloc/example/lib/firebase_options.dart b/packages/wyatt_authentication_bloc/example/lib/firebase_options.dart index 95cbbdfb..10987933 100644 --- a/packages/wyatt_authentication_bloc/example/lib/firebase_options.dart +++ b/packages/wyatt_authentication_bloc/example/lib/firebase_options.dart @@ -58,8 +58,10 @@ class DefaultFirebaseOptions { messagingSenderId: '405351917235', projectId: 'meerabel-dev', storageBucket: 'meerabel-dev.appspot.com', - androidClientId: '405351917235-4g1dh3475tq6t1sa2qoh7ol60nf4ta05.apps.googleusercontent.com', - iosClientId: '405351917235-2jv4ff02kovoim58f8d6d0rsa14apgkj.apps.googleusercontent.com', + androidClientId: + '405351917235-4g1dh3475tq6t1sa2qoh7ol60nf4ta05.apps.googleusercontent.com', + iosClientId: + '405351917235-2jv4ff02kovoim58f8d6d0rsa14apgkj.apps.googleusercontent.com', iosBundleId: 'com.example.exampleRouter', ); } diff --git a/packages/wyatt_authentication_bloc/lib/src/core/exceptions/exceptions.dart b/packages/wyatt_authentication_bloc/lib/src/core/exceptions/exceptions.dart index ba71af54..f60c915e 100644 --- a/packages/wyatt_authentication_bloc/lib/src/core/exceptions/exceptions.dart +++ b/packages/wyatt_authentication_bloc/lib/src/core/exceptions/exceptions.dart @@ -20,7 +20,6 @@ part 'exceptions_firebase.dart'; abstract class AuthenticationFailureInterface extends AppException implements Exception { - AuthenticationFailureInterface(this.code, this.msg); AuthenticationFailureInterface.fromCode(this.code) : msg = 'An unknown error occurred.'; @@ -277,3 +276,10 @@ abstract class UpdatePasswordFailureInterface UpdatePasswordFailureInterface.fromCode(super.code) : super.fromCode(); } + +abstract class ModelParsingFailureInterface + extends AuthenticationFailureInterface { + ModelParsingFailureInterface(super.code, super.msg); + + ModelParsingFailureInterface.fromCode(super.code) : super.fromCode(); +} diff --git a/packages/wyatt_authentication_bloc/lib/src/core/exceptions/exceptions_firebase.dart b/packages/wyatt_authentication_bloc/lib/src/core/exceptions/exceptions_firebase.dart index c36dcbd0..58e3c409 100644 --- a/packages/wyatt_authentication_bloc/lib/src/core/exceptions/exceptions_firebase.dart +++ b/packages/wyatt_authentication_bloc/lib/src/core/exceptions/exceptions_firebase.dart @@ -349,3 +349,10 @@ class UpdatePasswordFailureFirebase extends UpdatePasswordFailureInterface { } } } + +class ModelParsingFailureFirebase extends ModelParsingFailureInterface { + ModelParsingFailureFirebase([String? code, String? msg]) + : super(code ?? 'unknown', msg ?? 'An unknown error occurred.'); + + ModelParsingFailureFirebase.fromCode(super.code) : super.fromCode(); +} diff --git a/packages/wyatt_authentication_bloc/lib/src/data/data_sources/local/authentication_cache_data_source_impl.dart b/packages/wyatt_authentication_bloc/lib/src/data/data_sources/local/authentication_cache_data_source_impl.dart index b224575a..abcb673a 100644 --- a/packages/wyatt_authentication_bloc/lib/src/data/data_sources/local/authentication_cache_data_source_impl.dart +++ b/packages/wyatt_authentication_bloc/lib/src/data/data_sources/local/authentication_cache_data_source_impl.dart @@ -20,7 +20,6 @@ import 'package:wyatt_type_utils/wyatt_type_utils.dart'; class AuthenticationCacheDataSourceImpl extends AuthenticationCacheDataSource { - AuthenticationCacheDataSourceImpl(); Account? _account; T? _data; diff --git a/packages/wyatt_authentication_bloc/lib/src/data/data_sources/remote/authentication_firebase_data_source_impl.dart b/packages/wyatt_authentication_bloc/lib/src/data/data_sources/remote/authentication_firebase_data_source_impl.dart index 8a16490c..8a4ce432 100644 --- a/packages/wyatt_authentication_bloc/lib/src/data/data_sources/remote/authentication_firebase_data_source_impl.dart +++ b/packages/wyatt_authentication_bloc/lib/src/data/data_sources/remote/authentication_firebase_data_source_impl.dart @@ -14,36 +14,22 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +import 'package:wyatt_authentication_bloc/src/data/models/account_model_firebase.dart'; import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart'; import 'package:wyatt_type_utils/wyatt_type_utils.dart'; class AuthenticationFirebaseDataSourceImpl extends AuthenticationRemoteDataSource { - AuthenticationFirebaseDataSourceImpl( - {FirebaseAuth? firebaseAuth, GoogleSignIn? googleSignIn,}) - : _firebaseAuth = firebaseAuth ?? FirebaseAuth.instance, + AuthenticationFirebaseDataSourceImpl({ + FirebaseAuth? firebaseAuth, + GoogleSignIn? googleSignIn, + }) : _firebaseAuth = firebaseAuth ?? FirebaseAuth.instance, _googleSignIn = googleSignIn ?? GoogleSignIn(); final FirebaseAuth _firebaseAuth; final GoogleSignIn _googleSignIn; UserCredential? _latestCreds; - Account _mapper(User user) => AccountModel( - uid: user.uid, - emailVerified: user.emailVerified, - isAnonymous: user.isAnonymous, - providerId: user.providerData.first.providerId, - creationTime: user.metadata.creationTime, - lastSignInTime: user.metadata.lastSignInTime, - isNewUser: (user.metadata.creationTime != null && - user.metadata.lastSignInTime != null) - ? user.metadata.lastSignInTime! == user.metadata.creationTime! - : null, - email: user.email, - phoneNumber: user.phoneNumber, - photoURL: user.photoURL, - ); - @override Future signInWithEmailAndPassword({ required String email, @@ -56,11 +42,7 @@ class AuthenticationFirebaseDataSourceImpl ); _latestCreds = userCredential; final user = userCredential.user; - if (user.isNotNull) { - return _mapper(user!); - } else { - throw Exception(); // Get caught just after. - } + return AccountModelFirebase.fromFirebaseUser(user); } on FirebaseAuthException catch (e) { throw SignInWithEmailAndPasswordFailureFirebase.fromCode(e.code); } catch (_) { @@ -82,11 +64,7 @@ class AuthenticationFirebaseDataSourceImpl ); _latestCreds = userCredential; final user = userCredential.user; - if (user.isNotNull) { - return _mapper(user!); - } else { - throw Exception(); // Get caught just after. - } + return AccountModelFirebase.fromFirebaseUser(user); } on FirebaseAuthException catch (e) { throw SignUpWithEmailAndPasswordFailureFirebase.fromCode(e.code); } catch (_) { @@ -123,8 +101,11 @@ class AuthenticationFirebaseDataSourceImpl @override Stream streamAccount() => _firebaseAuth.userChanges().map((user) { - final Account? account = (user.isNotNull) ? _mapper(user!) : null; - return account; + try { + return AccountModelFirebase.fromFirebaseUser(user); + } on FirebaseAuthException { + return null; + } }); @override @@ -172,11 +153,7 @@ class AuthenticationFirebaseDataSourceImpl final userCredential = await _firebaseAuth.signInAnonymously(); _latestCreds = userCredential; final user = userCredential.user; - if (user.isNotNull) { - return _mapper(user!); - } else { - throw Exception(); // Get caught just after. - } + return AccountModelFirebase.fromFirebaseUser(user); } on FirebaseAuthException catch (e) { throw SignInAnonymouslyFailureFirebase.fromCode(e.code); } catch (_) { @@ -205,11 +182,7 @@ class AuthenticationFirebaseDataSourceImpl _latestCreds = userCredential; final user = userCredential.user; - if (user.isNotNull) { - return _mapper(user!); - } else { - throw Exception(); // Get caught just after. - } + return AccountModelFirebase.fromFirebaseUser(user); } on FirebaseAuthException catch (e) { throw SignInWithGoogleFailureFirebase.fromCode(e.code); } catch (_) { @@ -250,11 +223,7 @@ class AuthenticationFirebaseDataSourceImpl throw Exception(); // Get caught just after. } final user = _firebaseAuth.currentUser; - if (user.isNotNull) { - return _mapper(user!); - } else { - throw Exception(); // Get caught just after. - } + return AccountModelFirebase.fromFirebaseUser(user); } on FirebaseAuthException catch (e) { throw ReauthenticateFailureFirebase.fromCode(e.code); } catch (_) { @@ -267,11 +236,7 @@ class AuthenticationFirebaseDataSourceImpl try { await _firebaseAuth.currentUser!.updateEmail(email); final user = _firebaseAuth.currentUser; - if (user.isNotNull) { - return _mapper(user!); - } else { - throw Exception(); // Get caught just after. - } + return AccountModelFirebase.fromFirebaseUser(user); } on FirebaseAuthException catch (e) { throw UpdateEmailFailureFirebase.fromCode(e.code); } catch (_) { @@ -284,11 +249,7 @@ class AuthenticationFirebaseDataSourceImpl try { await _firebaseAuth.currentUser!.updatePassword(password); final user = _firebaseAuth.currentUser; - if (user.isNotNull) { - return _mapper(user!); - } else { - throw Exception(); // Get caught just after. - } + return AccountModelFirebase.fromFirebaseUser(user); } on FirebaseAuthException catch (e) { throw UpdatePasswordFailureFirebase.fromCode(e.code); } catch (_) { diff --git a/packages/wyatt_authentication_bloc/lib/src/data/data_sources/remote/authentication_mock_data_source_impl.dart b/packages/wyatt_authentication_bloc/lib/src/data/data_sources/remote/authentication_mock_data_source_impl.dart index 0c686ac6..1ee3a767 100644 --- a/packages/wyatt_authentication_bloc/lib/src/data/data_sources/remote/authentication_mock_data_source_impl.dart +++ b/packages/wyatt_authentication_bloc/lib/src/data/data_sources/remote/authentication_mock_data_source_impl.dart @@ -21,7 +21,6 @@ import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart'; import 'package:wyatt_type_utils/wyatt_type_utils.dart'; class AuthenticationMockDataSourceImpl extends AuthenticationRemoteDataSource { - AuthenticationMockDataSourceImpl({ this.idToken = 'fake-id-token', this.registeredAccounts, diff --git a/packages/wyatt_authentication_bloc/lib/src/data/models/account_model_firebase.dart b/packages/wyatt_authentication_bloc/lib/src/data/models/account_model_firebase.dart new file mode 100644 index 00000000..22313043 --- /dev/null +++ b/packages/wyatt_authentication_bloc/lib/src/data/models/account_model_firebase.dart @@ -0,0 +1,55 @@ +// ignore_for_file: public_member_api_docs, sort_constructors_first +// Copyright (C) 2022 WYATT GROUP +// Please see the AUTHORS file for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart'; + +class AccountModelFirebase extends AccountModel { + AccountModelFirebase._({ + required super.uid, + required super.emailVerified, + required super.isAnonymous, + required super.providerId, + super.lastSignInTime, + super.creationTime, + super.isNewUser, + super.email, + super.phoneNumber, + super.photoURL, + }); + + factory AccountModelFirebase.fromFirebaseUser(User? user) { + if (user != null) { + return AccountModelFirebase._( + uid: user.uid, + emailVerified: user.emailVerified, + isAnonymous: user.isAnonymous, + providerId: user.providerData.first.providerId, + creationTime: user.metadata.creationTime, + lastSignInTime: user.metadata.lastSignInTime, + isNewUser: (user.metadata.creationTime != null && + user.metadata.lastSignInTime != null) + ? user.metadata.lastSignInTime! == user.metadata.creationTime! + : null, + email: user.email, + phoneNumber: user.phoneNumber, + photoURL: user.photoURL, + ); + } else { + throw ModelParsingFailureFirebase('null-user', 'User cannot be null!'); + } + } +} diff --git a/packages/wyatt_authentication_bloc/lib/src/features/authentication/cubit/authentication_cubit.dart b/packages/wyatt_authentication_bloc/lib/src/features/authentication/cubit/authentication_cubit.dart index 5db96018..eab1ffbb 100644 --- a/packages/wyatt_authentication_bloc/lib/src/features/authentication/cubit/authentication_cubit.dart +++ b/packages/wyatt_authentication_bloc/lib/src/features/authentication/cubit/authentication_cubit.dart @@ -26,7 +26,6 @@ import 'package:wyatt_type_utils/wyatt_type_utils.dart'; part 'authentication_state.dart'; class AuthenticationCubit extends Cubit> { - AuthenticationCubit({ required AuthenticationRepository authenticationRepository, }) : _authenticationRepository = authenticationRepository, diff --git a/packages/wyatt_authentication_bloc/lib/src/features/authentication/cubit/authentication_state.dart b/packages/wyatt_authentication_bloc/lib/src/features/authentication/cubit/authentication_state.dart index b68660a0..a5733bf2 100644 --- a/packages/wyatt_authentication_bloc/lib/src/features/authentication/cubit/authentication_state.dart +++ b/packages/wyatt_authentication_bloc/lib/src/features/authentication/cubit/authentication_state.dart @@ -17,7 +17,6 @@ part of 'authentication_cubit.dart'; class AuthenticationState extends Equatable { - const AuthenticationState.unauthenticated() : this._(status: AuthenticationStatus.unauthenticated); diff --git a/packages/wyatt_authentication_bloc/lib/src/features/sign_up/cubit/sign_up_state.dart b/packages/wyatt_authentication_bloc/lib/src/features/sign_up/cubit/sign_up_state.dart index 414e9916..40c870c5 100644 --- a/packages/wyatt_authentication_bloc/lib/src/features/sign_up/cubit/sign_up_state.dart +++ b/packages/wyatt_authentication_bloc/lib/src/features/sign_up/cubit/sign_up_state.dart @@ -17,7 +17,6 @@ part of 'sign_up_cubit.dart'; class SignUpState extends FormDataState { - const SignUpState({ required super.form, super.status = FormStatus.pure,