我正在开发一个 Flutter 应用程序,用户可以使用他们的 Google 帐户登录。我已经实现了 Google 登录功能,但我遇到了导航流程和身份验证问题。
当新用户登录 Google 时,他们应该被引导至注册页面,可以在其中提供其他信息,例如联系电话、商店名称和地址。提供此信息后,应将其存储在 Firestore 中。 现在,下次当用户使用相同的 Google 帐户登录时,他应该被定向到主页,在本例中为 btmnavbar
但是,我无法实现这个目标。下面的代码确实在 Firestore 中注册了所有必需的详细信息,但下次当用户使用同一电子邮件登录时,它会再次导航到注册页面并询问信息 在注册页面中,详细信息应存储在登录页面中选择的同一电子邮件
登录页面 sinin 功能
class _LoginAppState extends State<LoginApp> {
ScrollController scrollController = ScrollController();
bool isHidden = true;
bool isLoading = false;
TextEditingController controllerEmail = TextEditingController();
TextEditingController controllerPassword = TextEditingController();
final GoogleSignIn googleSignIn = GoogleSignIn();
TextEditingController resetPassController = TextEditingController();
final auth = FirebaseAuth.instance;
final GoogleSignIn _googleSignIn = GoogleSignIn();
/////////////////////////////////////////////////////////////
//////////Function for Google SIGN IN/////////////////////
///////////////////////////////////////////////////
Future<User?> signingoogle() async {
try {
// GoogleSignIn().signOut();
final GoogleSignInAccount? googleaccount = await GoogleSignIn().signIn();
if (googleaccount == null) {
return null;
}
print('Creating Google Auth');
final GoogleSignInAuthentication googleAuth =
await googleaccount.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
final UserCredential userCredential =
await FirebaseAuth.instance.signInWithCredential(credential);
final User? user = userCredential.user;
if (user != null) {
final CollectionReference users =
FirebaseFirestore.instance.collection('users');
final DocumentSnapshot emailDocument = await users.doc(user.uid).get();
if (emailDocument.exists) {
Navigator.push(
context, MaterialPageRoute(builder: (context) => btmNavBar()));
} else {
await users.doc(user.uid).set({'email': user.email});
final AuthCredential emailAuthCredential =
EmailAuthProvider.credential(
email: user.email.toString(), password: 'google password');
await userCredential.user!.linkWithCredential(emailAuthCredential);
}
Navigator.push(
context, MaterialPageRoute(builder: (context) => SignUpGoogle()));
}
return user;
} catch (e) {
print('$e');
}
return null;
}
这是我的注册页面功能
class _SignUpGoogleState extends State<SignUpGoogle> {
TextEditingController contactController = TextEditingController();
TextEditingController storeNameController = TextEditingController();
TextEditingController addressController = TextEditingController();
final formKey = GlobalKey<FormState>();
final GoogleSignIn _googleSignIn = GoogleSignIn();
final auth = FirebaseAuth.instance;
final firestore = FirebaseFirestore.instance;
User? currentUser = FirebaseAuth.instance.currentUser;
Future<void> saveUserData(String userId, String email, String contact,
String storeName, String address) async {
try {
// Save user data to Firestore
await firestore.collection('users').doc(userId).set({
'email': email,
'contact': contact,
'storeName': storeName,
'address': address,
});
} catch (e) {
print('Error saving user data: $e');
}
}
SecondayBtn(
onPressed: () async {
var userContact = contactController.text.trim();
var storeName = storeNameController.text.trim();
var userAddress = addressController.text.trim();
if (formKey.currentState!.validate()) {
try {
final GoogleSignInAccount? googleSignInAccount =
await _googleSignIn.signIn();
if (googleSignInAccount != null) {
final GoogleSignInAuthentication
googleSignInAuthentication =
await googleSignInAccount.authentication;
final AuthCredential credential =
GoogleAuthProvider.credential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
final UserCredential userCredential =
await auth.signInWithCredential(credential);
final User? user = userCredential.user;
if (user != null) {
// Save additional user data to Firestore
await saveUserData(user.uid, user.email!,
userContact, storeName, userAddress);
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => btmNavBar()),
);
}
}
} catch (e) {}
}
},
btntxt: 'Sign Up With Google',
),
当新用户登录 Google 时,他们应该被引导至注册页面,可以在其中提供其他信息,例如联系电话、商店名称和地址。提供此信息后,应将其存储在 Firestore 中。 现在,下次当用户使用相同的 Google 帐户登录时,他应该被定向到主页,在本例中为 btmnavbar
逻辑处理代码似乎存在问题,决定在
signingoogle()
方法内导航到何处。无论您正在处理什么,它始终会导航到 SignUpGoogle 页面。
if (user != null) {
final CollectionReference users =
FirebaseFirestore.instance.collection('users');
final DocumentSnapshot emailDocument = await users.doc(user.uid).get();
if (emailDocument.exists) {
Navigator.push(
context, MaterialPageRoute(builder: (context) => btmNavBar()));
} else {
await users.doc(user.uid).set({'email': user.email});
final AuthCredential emailAuthCredential =
EmailAuthProvider.credential(
email: user.email.toString(), password: 'google password');
await userCredential.user!.linkWithCredential(emailAuthCredential);
}
Navigator.push(
context, MaterialPageRoute(builder: (context) => SignUpGoogle())); //<<< Here! This is outside of the if-else statement
}