user property

Future<User?> user

Returns the current authenticated user.

If the user is not authenticated, authentication to Firebase is done according to the specified FireBaseAuthenticationMethods:

  • GOOGLE -- authenticate using Google credentials
  • PASSWORD - authenticate using username and password

Returns null if the user cannot be authenticated.

Implementation

Future<User?> get user async {
  if (firebaseEndPoint == null)
    throw CarpFirebaseBackendException(
        "The Firebase Endpoint is not configured - call the 'initialize()' method first.");

  if (_user == null) {
    switch (firebaseEndPoint!.firebaseAuthenticationMethod) {
      case FireBaseAuthenticationMethods.GOOGLE:
        {
          GoogleSignInAccount googleUser =
              await (_googleSignIn.signIn() as FutureOr<GoogleSignInAccount>);
          GoogleSignInAuthentication googleAuth =
              await googleUser.authentication;
          final AuthCredential credential = GoogleAuthProvider.credential(
            accessToken: googleAuth.accessToken,
            idToken: googleAuth.idToken,
          );
          UserCredential result =
              await _auth.signInWithCredential(credential);
          _user = result.user;
          break;
        }
      case FireBaseAuthenticationMethods.PASSWORD:
        {
          assert(firebaseEndPoint?.email != null);
          assert(firebaseEndPoint?.password != null);
          UserCredential result = await _auth.signInWithEmailAndPassword(
              email: firebaseEndPoint!.email!,
              password: firebaseEndPoint!.password!);
          _user = result.user;
          break;
        }
      default:
        {
          //TODO : should probably throw a NotImplementedException
        }
    }
    if (_user != null) {
      addEvent(FirebaseDataManagerEvent(
          FirebaseDataManagerEventTypes.authenticated));
      print("signed in as " + _user!.email! + " - uid: " + _user!.uid);
    }
  }

  return _user;
}