First thing first, I'm not sure if the I've put the title right for this question, so feel free to correct me if you know another one better then mine.

WHAT IM'M TRYING TO DO:

I'm trying to implement an authentification system based on firebase using Provider state management. But I encounter this problem...

So I want to have an abstract class called BaseAuth where I will have all the function definitions that I need without interfering with any Firebase. Because I want to keep them how separated I can, here it's a tricky part.

BaseAuth:

abstract class BaseAuth {
  Future<String> login(String email, String password);
  Future<String> register(String email, String password);
  Future<void> onAuthStateChanged(void Function(FirebaseUser) callback);
  Future<void> logout();
  Future<UserData> getCurrentUser();
}

DatabaseFirebase:

class DatabaseFirebase implements BaseAuth {
  final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
  FirebaseUser _firebaseUser;

  //Some implementations bla bla bla

  @override
  Future<void> onAuthStateChanged(void Function(FirebaseUser) callback) async {
    _firebaseAuth.onAuthStateChanged.listen(callback);
  }
}

UserRepository:

class UserRepository with ChangeNotifier {
  Status _status = Status.Uninitialized;
  BaseAuth auth;
  UserData _user;

  UserRepository({@required this.auth}) {
    auth.onAuthStateChanged(_onAuthStateChanged);
  }
  //Some implementation bla bla bla

  Future<void> _onAuthStateChanged(FirebaseUser user) async {
    if (user == null || user.uid == null) {
      _status = Status.Unauthenticated;
    } else {
      auth.getCurrentUser().then((user) => _user = user);
      _status = Status.Authenticated;
    }
    notifyListeners();
  }
}

So how you can see in order for my login system to work I'm forced to use FirebaseUser over there in that function. Do you have any idea how can I separate this function? I want to have in my BaseAuth a function called onAuthStateChanged without any parameter or with one called UserData which is my user model.

So in other words, is there any way for me to make my BaseAuth class independent from firebase? How should my function look in order to work after refactoring?

0 Answers