refactor(authentication): use fromFirebaseUser factory to parse user
This commit is contained in:
parent
f46707f5c1
commit
5f52b2fc3d
@ -50,8 +50,8 @@ abstract class GetItInitializer {
|
||||
),
|
||||
])
|
||||
: () => AuthenticationFirebaseDataSourceImpl(
|
||||
googleSignIn: GoogleSignIn(clientId: DefaultFirebaseOptions.ios.iosClientId)
|
||||
),
|
||||
googleSignIn: GoogleSignIn(
|
||||
clientId: DefaultFirebaseOptions.ios.iosClientId)),
|
||||
)
|
||||
..registerLazySingleton<AuthenticationCacheDataSource<int>>(
|
||||
() => AuthenticationCacheDataSourceImpl<int>(),
|
||||
|
@ -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',
|
||||
);
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -20,7 +20,6 @@ import 'package:wyatt_type_utils/wyatt_type_utils.dart';
|
||||
|
||||
class AuthenticationCacheDataSourceImpl<T extends Object>
|
||||
extends AuthenticationCacheDataSource<T> {
|
||||
|
||||
AuthenticationCacheDataSourceImpl();
|
||||
Account? _account;
|
||||
T? _data;
|
||||
|
@ -14,36 +14,22 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
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<Account> 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<Account?> streamAccount() =>
|
||||
_firebaseAuth.userChanges().map<Account?>((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 (_) {
|
||||
|
@ -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,
|
||||
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
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!');
|
||||
}
|
||||
}
|
||||
}
|
@ -26,7 +26,6 @@ import 'package:wyatt_type_utils/wyatt_type_utils.dart';
|
||||
part 'authentication_state.dart';
|
||||
|
||||
class AuthenticationCubit<Extra> extends Cubit<AuthenticationState<Extra>> {
|
||||
|
||||
AuthenticationCubit({
|
||||
required AuthenticationRepository<Extra> authenticationRepository,
|
||||
}) : _authenticationRepository = authenticationRepository,
|
||||
|
@ -17,7 +17,6 @@
|
||||
part of 'authentication_cubit.dart';
|
||||
|
||||
class AuthenticationState<Extra> extends Equatable {
|
||||
|
||||
const AuthenticationState.unauthenticated()
|
||||
: this._(status: AuthenticationStatus.unauthenticated);
|
||||
|
||||
|
@ -17,7 +17,6 @@
|
||||
part of 'sign_up_cubit.dart';
|
||||
|
||||
class SignUpState extends FormDataState {
|
||||
|
||||
const SignUpState({
|
||||
required super.form,
|
||||
super.status = FormStatus.pure,
|
||||
|
Loading…
x
Reference in New Issue
Block a user