Reputation: 107
I want to check if the user is new or not by using Firebase. I have a button to SignIn with google
Future<FirebaseUser> googleSignIn() async {
try {
loading.add(true);
GoogleSignInAccount googleSignInAccount = await _googleSignIn.signIn();
GoogleSignInAuthentication googleAuth =
await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
FirebaseUser user = (await _auth.signInWithCredential(credential)).user;
loading.add(false);
return user;
} catch (error) {
return error;
}
}
If I add FirebaseUserMetadata I have this : A value of type 'Future' can't be assigned to a variable of type 'FirebaseUserMetadata'. Try changing the type of the variable, or casting the right-hand type to 'FirebaseUserMetadata'.
I think what I have to do is to compare lastSignInTimestamp and creationTimestamp from FirebaseUserMetadata.
FirebaseUser user = (await _auth.signInWithCredential(credential)).user;
if(new user){
updateDataNewUser(...)
}
else{
updateDataUser(...)
}
How I can get this data from FirebaseUserMetadata in my case ?
Thank you.
Upvotes: 0
Views: 202
Reputation: 167
private fun firebaseAuthWithGoogle(acct: GoogleSignInAccount) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.id!!)
showProgressDialog()
val credential = GoogleAuthProvider.getCredential(acct.idToken, null)
mAuth!!.signInWithCredential(credential)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
firebaseUser = mAuth!!.currentUser
FirebaseInstanceId.getInstance().instanceId
.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
return@OnCompleteListener
}
// Get new Instance ID token
// this is a new user
buildThatNoob()
})
} else {
OurToast.myToast(this, "Something went Wrong ... try again later")
val i = Intent(baseContext, Launcher::class.java)
startActivity(i)
finish()
}
hideProgressDialog()
}
}
Upvotes: 0
Reputation: 80934
To check if the user is new or not, try the following:
AuthResult user = await auth.signInWithCredential(credential);
print(user.additionalUserInfo.isNewUser);
isNewUser
returns whether the user is new or existing
Upvotes: 1