Reputation: 11
I want to check whether the user has signed up before or not (if this is his first time I will send him to write some data if not he will enter to the main page of the app).
GoogleSignInAccount _user;
GoogleSignInAccount get user => _user;
Future googlelogin() async {
try {
final googleUser = await googleSignIn.signIn();
if (googleUser == null) return;
_user = googleUser;
final googleAuth = await googleUser.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
await FirebaseAuth.instance.signInWithCredential(credential);
} catch (e) {
print(e.toString());
}
notifyListeners();
}
I also have a StreamBuilder to check the data that user enter
StreamBuilder<Object>(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator.adaptive(),
);
} else if (snapshot.hasData) {
return LoggedInWidget();
} else if (snapshot.hasError) {
return Center(
child: Text('Something went wrong'),
);
} else {
return SignUp();
}
})
Upvotes: 1
Views: 1124
Reputation: 87
You can use the pre-built method "isNewUser" of the "UserCredential" class. Call the sing in with credentials function from a variable and use said variable to perform the check.
...
var result =
await FirebaseAuth.instance.signInWithCredential(credential);
if (result.additionalUserInfo!.isNewUser) {
// Perform what you need to do for new users here
// like creating a user document
}else {
//Perform what you want to do for old users here
//like fetching a specific user document
}
...
Upvotes: 2