Search
[Flutter/dart] Determine if the login is first login in firebase_auth
- M.R

- Aug 8, 2021
- 1 min read
Summary
If you are using the login function with a google account etc. in the app, you may want to determine whether it is the first login. For example, when a user log in for the first time, you can create a database for that user.
This time, I will introduce how to determine the first login when using the google login of firebase_auth.
Method
The version of firebase_auth is 0.18.4.
UserCredential > additionalUserInfo > isNewUser
contains information about whether a user are logged in for the first time. IsNewUser will be true for the first login.
Methods like signInWithCredential(), signInAnonymously() return the instance of UserCredential and you can use it.
//Authenticate user with google account
Future<UserCredential> authGoogleUser(FirebaseAuth auth,
GoogleSignInAccount googleAccount) async{
GoogleSignInAuthentication _googleAuth=await
googleAccount.authentication;
final AuthCredential _credential=GoogleAuthProvider.credential(
accessToken: _googleAuth.accessToken, idToken: _googleAuth.idToken
);
UserCredential cred=await auth.signInWithCredential(_credential);
user=cred.user; //User information
isFirst=cred.additionalUserInfo.isNewUser; //wheher first login
return cred;
}






Comments