I have wrote a code, which should avoid the automatically login when an user get created.
The code worked until I used the dart migration tool. Now when I use this code with null-safety, then the secondaryApp is not created and another user get logged in.
Can someone please explain me, what I am doing wrong? I need to fix this code as fast as possible, but I just can't find the mistake.
I really appreciate any help.
code login:
InkWell(
onTap: () async {
await authProvider.signUpUser(
usernameController.text.trim(),
emailController.text.trim(),
passwordController.text.trim(),
firstNameController.text.trim(),
lastNameController.text.trim(),
_birthDateInString,
_genderSelected);
})
code (database request):
Future<bool> signUpUser(String username, String email, String password,
String firstName, String lastName, String? birthday, String? gender) async {
try {
_status = Status.Authenticating;
notifyListeners(); //changing status
FirebaseApp secondaryApp = await Firebase.initializeApp(
name: 'Secondary',
options: Firebase.app().options,
);
try {
UserCredential? credential = await FirebaseAuth.instanceFor(
app: secondaryApp)
.createUserWithEmailAndPassword(email: email, password: password)
.then((result) async {
//User user = result.user;
_userServices.createUser(
uid: result.user!.uid,
username: username,
email: email,
firstName: firstName,
lastName: lastName,
birthday: birthday,
gender: gender,
status: 'aktiv',
role: 'User',
);
});
print('user created');
await credential?.user!.sendEmailVerification();
} on FirebaseAuthException catch (e) {
print(e);
}
print('secondapp deleted');
await secondaryApp.delete();
_status = Status.Unauthenticated;
notifyListeners();
print('secondapp deleted');
return true;
} catch (e) {
_status = Status.Unauthenticated;
notifyListeners();
print(e.toString());
return false;
}
}
CodePudding user response:
A little difficult to completely debug your project without a little more context but I tried to clean up just a bit and add a few print statements to help you out, hope this helps :)
Future<bool> signUpUser(
String username,
String email,
String password,
String firstName,
String lastName,
String? birthday,
String? gender,
) async {
try {
_status = Status.Authenticating;
notifyListeners(); //changing status
FirebaseApp secondaryApp = await Firebase.initializeApp(
name: 'Secondary',
options: Firebase.app().options,
);
/// here is our user:)
var _user;
try {
UserCredential? credential = await FirebaseAuth.instanceFor(app: secondaryApp)
.createUserWithEmailAndPassword(email: email, password: password);
print('we got our credentials lets take a look to see if we have them ${credential}');
var uid = credential.user?.uid;
if (uid != null) {
_userServices.createUser(
uid: uid,
username: username,
email: email,
firstName: firstName,
lastName: lastName,
birthday: birthday,
gender: gender,
status: 'aktiv',
role: 'User',
);
print('user created');
}
_user = credential?.user;
if (_user != null) {
await credential?.user.sendEmailVerification();
print('done with our send email verification!');
}
} on FirebaseAuthException catch (e) {
print(e);
}
if (_user != null) {
/// this means that we successfully authenticated our user consider returning from the function right here!!
}
print('secondapp deleted');
await secondaryApp.delete();
_status = Status.Unauthenticated;
notifyListeners();
print('secondapp deleted');
return true;
} catch (e) {
_status = Status.Unauthenticated;
notifyListeners();
print(e.toString());
return false;
}
}
One other thing that I noticed with your code is that if a User successfully authenticates then you automatically delete your secondaryApp. Is this your desired behavior?
I went ahead and added a note for you to add some code in there if you want but didn't want to dramatically change your code.