I am trying to create a class for adding a user to the Firebase firestore by using the Google email/password. I have the code as part of another screen but I want to pull it out and create a class. I’m not sure how I can do this.
I have tried to create a class but it is more complicated than I know how to do.
I want to pass the email and password to the class but I am not sure how to do that with what I have done.
I also use a snackbar to show if there are any errors creating the account but I don’t know how I can include all of this in a class.
I am making a mess of this class. It works if I don’t try to make a class out of it so maybe I should just leave it?
Here is the code:
class UserRegistration extends ConsumerStatefulWidget {
final String email;
final String password;
final _auth = FirebaseAuth.instance;
bool showSpinner = false;
bool registrationFail = false;
String errorMessage = "";
@override
void register_user() {
try {
final newUser = await _auth.createUserWithEmailAndPassword(
email: widget.email, password: widget.password);
if (newUser != null) {
ref
.read(usersNotifierProvider.notifier)
.updateuserID(newUser.user!.uid);
ref
.read(usersNotifierProvider.notifier)
.updateEmail(newUser.user!.email!);
ref.read(globalsNotifierProvider.notifier).updatenewUser(true);
ref.read(globalsNotifierProvider.notifier).updatenewCompany(true);
/// Send the email verification
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) => const VerifyEmailScreen()));
} else {
setState(() {
registrationFail = true;
});
}
setState(() {
showSpinner = false;
});
} on FirebaseAuthException catch (error) {
switch (error.code) {
case "ERROR_INVALID_EMAIL":
case "invalid-email":
errorMessage = "Your email address appears to be malformed.";
break;
case "email-already-in-use":
errorMessage = "Email is already in use.";
break;
case "ERROR_WRONG_PASSWORD":
case "wrong-password":
errorMessage = "Your password is wrong.";
break;
case "weak-password":
errorMessage = "Weak password";
break;
case "ERROR_USER_NOT_FOUND":
case "user-not-found":
errorMessage = "User with this email doesn't exist.";
break;
case "ERROR_USER_DISABLED":
case "user-disabled":
errorMessage = "User with this email has been disabled.";
break;
case "ERROR_TOO_MANY_REQUESTS":
case "too-many-requests":
errorMessage = "Too many requests. Try again later.";
break;
case "ERROR_OPERATION_NOT_ALLOWED":
case "operation-not-allowed":
errorMessage = "Signing in with Email and Password is not enabled.";
break;
default:
errorMessage = "An undefined Error happened. Please try again.";
}
if (errorMessage != "") {
ScaffoldMessenger.of(context).showSnackBar((SnackBar(
content: Center(
child: Text(
errorMessage,
style: const TextStyle(
color: Colors.black, fontWeight: FontWeight.w900),
),
),
behavior: SnackBarBehavior.floating,
margin: EdgeInsets.only(
bottom: MediaQuery.of(context).size.height - 100,
left: 10,
right: 10,
),
backgroundColor: Colors.redAccent,
)));
}
}
}
}
Any help you can give me would be very much appreciated. Thanks
4