Skip to content

Instantly share code, notes, and snippets.

View NoaTubic's full-sized avatar

Noa Tubic NoaTubic

  • Four of them, AlphaGenesis Projects, OmniThread
  • Croatia
  • 13:09 (UTC +02:00)
View GitHub Profile
abstract class AuthRepository {
Future<Either<AuthFailure, Unit>> signInWithEmailAndPassword({
required EmailAddress emailAddress,
required Password password,
});
}
@LazySingleton(as: AuthRepository)
class AuthRepositoryImpl implements AuthRepository {
final FirebaseAuth _firebaseAuth;
AuthRepositoryImpl(this._firebaseAuth);
@override
Future<Either<AuthFailure, Unit>> signInWithEmailAndPassword({
required EmailAddress emailAddress,
required Password password,
Future<User> signInOrCreateAccount(
BuildContext context, Future<UserCredential> Function() signInFunc) async {
try {
final userCredential = await signInFunc();
await maybeCreateUser(userCredential.user);
return userCredential.user;
} on FirebaseAuthException catch (e) {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Error: ${e.message}')),
Future<User> signInWithEmail(
BuildContext context, String email, String password) async {
final signInFunc = () => FirebaseAuth.instance
.signInWithEmailAndPassword(email: email.trim(), password: password);
return signInOrCreateAccount(context, signInFunc);
}