更新时间:2022-06-26 07:49:03
您可以通过User Credential
检查该用户是新用户还是先前注册的用户,如果还不是该用户,则可以检索authResult.additionalUserInfo.isNewUser
之前注册过.
You can check the user whether he is a new or previously registered user via User Credential
and through you can retrieve the authResult.additionalUserInfo.isNewUser
if the user is not already registered before.
您可以使用以下代码:
Future<UserCredential> signInWithGoogle(BuildContext context) async {
FirebaseAuth _auth = FirebaseAuth.instance;
// Trigger the authentication flow
final GoogleSignInAccount googleUser = await GoogleSignIn().signIn();
// Obtain the auth details from the request
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
// Create a new credential
final GoogleAuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
final UserCredential authResult =
await _auth.signInWithCredential(credential);
final User user = authResult.user;
///Her to check isNewUser OR Not
if (authResult.additionalUserInfo.isNewUser) {
if (user != null) {
//You can her set data user in Fire store
//Ex: Go to RegisterPage()
}
}
} else {
//Ex: Go to HomePage()
}
return await FirebaseAuth.instance.signInWithCredential(credential);
}