performOAuthLogin static method

Future<User?> performOAuthLogin({
  1. required String provider,
  2. List<String>? scopes,
  3. Map<String, String>? parameters,
  4. dynamic onError(
    1. String
    )?,
})

This function is responsible to conduct the OAuthLoginFlow and handle all the errors associated with it.

Implementation

static Future<User?> performOAuthLogin({
  required String provider,
  List<String>? scopes,
  Map<String, String>? parameters,
  Function(String)? onError,
}) async {
  //Reduces Boilerplate! Basically Handles all the errors that occur in the OAuthFlow
  void handleError(String err) {
    if (err.contains(AuthErrors.operationCancelled)) {
      print("OPERATION CANCELLED OR INTERRUPTED");
      Fluttertoast.showToast(
        msg: "OPERATION CANCELLED OR INTERRUPTED",
        toastLength: Toast.LENGTH_LONG,
        gravity: ToastGravity.BOTTOM,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0,
      );
    }
    if (err.contains(AuthErrors.callbackNotApproved)) {
      err =
          "Firebase Twitter Auth callbackURL not added to your Twitter Developer Project's (3-legged OAuth) Authentication Settings";
      debugPrint(err);
      if (onError != null) onError(err);
    } else {
      debugPrint(err);
      if (onError != null) onError(err);
    }
  }

  User? usr;
  try {
    usr = await FirebaseAuthOAuth().openSignInFlow(
      provider,
      scopes ?? ["email"],
      parameters ?? {"locale": "en"},
    );
  } on PlatformException catch (error) {
    handleError("${error.code}: ${error.message}");
  } catch (e) {
    handleError(e.toString());
  }
  return usr;
}