I’m working on a group project in which user can create two types of accounts. A Public and a Private. if the user create Public account his/her private account will also be created and if he creates private account then only the private account will be created.
Keepin in mind that I’m creating two collections in firebase “publicAccounts” and “privateAccounts”.
now the issue is if the user want to switch from public account to the private account which also has the exact same “id” in a different collection then how will it be loged in?
Heres what I’m trying to do but it again login to the user of same collection
void SwitchAccount() async {
final FirebaseAuth _auth = FirebaseAuth.instance;
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
final User? _user = _auth.currentUser;
if (_user != null) {
final String _userId = _user.phoneNumber ?? _user.email ?? "";
// Log out the current user from Citizens collection
await _auth.signOut();
// Log in with the same ID but in the context of publicData collection
DocumentSnapshot pubDoc =
await _firestore.collection("publicData").doc(_userId).get();
if (lawyerDoc.exists) {
UserCredential? userCredential = await loginWithGoogle();
if (userCredential != null) {
Utils().Message("Switching to Public Profile.");
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => home_screen()));
} else {
Utils().Message("Login to professional profile failed.");
}
} else {
Utils().Message("No Public Account Registered on current ID.");
}
}
}
Future<UserCredential?> loginWithGoogle() async {
try {
final googleUser = await GoogleSignIn().signIn();
if (googleUser == null) {
return null; // The user canceled the sign-in
}
final googleAuth = await googleUser.authentication;
final cred = GoogleAuthProvider.credential(
idToken: googleAuth.idToken,
accessToken: googleAuth.accessToken,
);
return await FirebaseAuth.instance.signInWithCredential(cred);
} catch (e) {
Utils().Message(e.toString());
}
return null;
}
Misbah Khan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.