[Flutter/dart] Determine if the login is first login in firebase_auth
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;
}
Recent Posts
See AllWhat want to do I want to create an input form using TextField. For example, if the input content is a monetary amount, I would like to...
What want to do There is a variable that remain unchanged once the initial value is determined. However, it cannot be determined yet when...
What want to do As the title suggests. Place two widgets in one line on the screen One in the center of the screen and the other on the...
Comments