Firebase user registration with google












0














I am trying to implement google sign-in feature in my app. I have followed this documentation, problem is - if I use previously used google account, it is not showing an error like "the e-mail is already registered in this app". As a result, previously stored data with the account are deleted & the google account is registered again. Here is my code :



private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());

AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
int coins = 100;
String name, email;
name = user.getDisplayName();
email = user.getEmail();
//updateUI(user);
storeUserCredentials(name, email, coins);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
Toast.makeText(SignUpActivity.this, "Error : " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}

public void storeUserCredentials(String name, String email, int coins){
progressBar.setVisibility(View.VISIBLE);
User user = new User(name, email, coins);

mDatabase.getReference("Users")
.child(mAuth.getCurrentUser().getUid())
.setValue(user)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
progressBar.setVisibility(View.GONE);
if (task.isSuccessful()){
Toast.makeText(SignUpActivity.this, "Registration Successful!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(SignUpActivity.this, MainActivity.class));
finish();
} else {
if (task.getException() instanceof FirebaseAuthUserCollisionException){
Toast.makeText(SignUpActivity.this, "The email is already used", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(SignUpActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}

}
}
});
}


What is wrong with my code, please explain.










share|improve this question
























  • Are you asking why this code succeeds? mDatabase.getReference("Users") .child(mAuth.getCurrentUser().getUid()) .setValue(user)
    – Frank van Puffelen
    Nov 12 at 15:15










  • Actually I'm asking why storeUserCredentials() gets called. As the google account is already registered, storeUserCredentials() should not be called again. Probably I am missing something, but I cant figure it out.
    – N.K.T
    Nov 12 at 15:20






  • 2




    You're calling signInWithCredential, which successfully completes then the user is signed in.
    – Frank van Puffelen
    Nov 12 at 15:29












  • The method is called because signInWithCredential is triggered always, not only the first time the user logs in the app
    – bra_racing
    Nov 12 at 15:32
















0














I am trying to implement google sign-in feature in my app. I have followed this documentation, problem is - if I use previously used google account, it is not showing an error like "the e-mail is already registered in this app". As a result, previously stored data with the account are deleted & the google account is registered again. Here is my code :



private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());

AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
int coins = 100;
String name, email;
name = user.getDisplayName();
email = user.getEmail();
//updateUI(user);
storeUserCredentials(name, email, coins);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
Toast.makeText(SignUpActivity.this, "Error : " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}

public void storeUserCredentials(String name, String email, int coins){
progressBar.setVisibility(View.VISIBLE);
User user = new User(name, email, coins);

mDatabase.getReference("Users")
.child(mAuth.getCurrentUser().getUid())
.setValue(user)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
progressBar.setVisibility(View.GONE);
if (task.isSuccessful()){
Toast.makeText(SignUpActivity.this, "Registration Successful!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(SignUpActivity.this, MainActivity.class));
finish();
} else {
if (task.getException() instanceof FirebaseAuthUserCollisionException){
Toast.makeText(SignUpActivity.this, "The email is already used", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(SignUpActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}

}
}
});
}


What is wrong with my code, please explain.










share|improve this question
























  • Are you asking why this code succeeds? mDatabase.getReference("Users") .child(mAuth.getCurrentUser().getUid()) .setValue(user)
    – Frank van Puffelen
    Nov 12 at 15:15










  • Actually I'm asking why storeUserCredentials() gets called. As the google account is already registered, storeUserCredentials() should not be called again. Probably I am missing something, but I cant figure it out.
    – N.K.T
    Nov 12 at 15:20






  • 2




    You're calling signInWithCredential, which successfully completes then the user is signed in.
    – Frank van Puffelen
    Nov 12 at 15:29












  • The method is called because signInWithCredential is triggered always, not only the first time the user logs in the app
    – bra_racing
    Nov 12 at 15:32














0












0








0







I am trying to implement google sign-in feature in my app. I have followed this documentation, problem is - if I use previously used google account, it is not showing an error like "the e-mail is already registered in this app". As a result, previously stored data with the account are deleted & the google account is registered again. Here is my code :



private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());

AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
int coins = 100;
String name, email;
name = user.getDisplayName();
email = user.getEmail();
//updateUI(user);
storeUserCredentials(name, email, coins);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
Toast.makeText(SignUpActivity.this, "Error : " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}

public void storeUserCredentials(String name, String email, int coins){
progressBar.setVisibility(View.VISIBLE);
User user = new User(name, email, coins);

mDatabase.getReference("Users")
.child(mAuth.getCurrentUser().getUid())
.setValue(user)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
progressBar.setVisibility(View.GONE);
if (task.isSuccessful()){
Toast.makeText(SignUpActivity.this, "Registration Successful!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(SignUpActivity.this, MainActivity.class));
finish();
} else {
if (task.getException() instanceof FirebaseAuthUserCollisionException){
Toast.makeText(SignUpActivity.this, "The email is already used", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(SignUpActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}

}
}
});
}


What is wrong with my code, please explain.










share|improve this question















I am trying to implement google sign-in feature in my app. I have followed this documentation, problem is - if I use previously used google account, it is not showing an error like "the e-mail is already registered in this app". As a result, previously stored data with the account are deleted & the google account is registered again. Here is my code :



private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());

AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
int coins = 100;
String name, email;
name = user.getDisplayName();
email = user.getEmail();
//updateUI(user);
storeUserCredentials(name, email, coins);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
Toast.makeText(SignUpActivity.this, "Error : " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}

public void storeUserCredentials(String name, String email, int coins){
progressBar.setVisibility(View.VISIBLE);
User user = new User(name, email, coins);

mDatabase.getReference("Users")
.child(mAuth.getCurrentUser().getUid())
.setValue(user)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
progressBar.setVisibility(View.GONE);
if (task.isSuccessful()){
Toast.makeText(SignUpActivity.this, "Registration Successful!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(SignUpActivity.this, MainActivity.class));
finish();
} else {
if (task.getException() instanceof FirebaseAuthUserCollisionException){
Toast.makeText(SignUpActivity.this, "The email is already used", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(SignUpActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}

}
}
});
}


What is wrong with my code, please explain.







java android firebase firebase-authentication google-signin






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 15:04









Fantômas

32.3k156288




32.3k156288










asked Nov 12 at 14:20









N.K.T

102314




102314












  • Are you asking why this code succeeds? mDatabase.getReference("Users") .child(mAuth.getCurrentUser().getUid()) .setValue(user)
    – Frank van Puffelen
    Nov 12 at 15:15










  • Actually I'm asking why storeUserCredentials() gets called. As the google account is already registered, storeUserCredentials() should not be called again. Probably I am missing something, but I cant figure it out.
    – N.K.T
    Nov 12 at 15:20






  • 2




    You're calling signInWithCredential, which successfully completes then the user is signed in.
    – Frank van Puffelen
    Nov 12 at 15:29












  • The method is called because signInWithCredential is triggered always, not only the first time the user logs in the app
    – bra_racing
    Nov 12 at 15:32


















  • Are you asking why this code succeeds? mDatabase.getReference("Users") .child(mAuth.getCurrentUser().getUid()) .setValue(user)
    – Frank van Puffelen
    Nov 12 at 15:15










  • Actually I'm asking why storeUserCredentials() gets called. As the google account is already registered, storeUserCredentials() should not be called again. Probably I am missing something, but I cant figure it out.
    – N.K.T
    Nov 12 at 15:20






  • 2




    You're calling signInWithCredential, which successfully completes then the user is signed in.
    – Frank van Puffelen
    Nov 12 at 15:29












  • The method is called because signInWithCredential is triggered always, not only the first time the user logs in the app
    – bra_racing
    Nov 12 at 15:32
















Are you asking why this code succeeds? mDatabase.getReference("Users") .child(mAuth.getCurrentUser().getUid()) .setValue(user)
– Frank van Puffelen
Nov 12 at 15:15




Are you asking why this code succeeds? mDatabase.getReference("Users") .child(mAuth.getCurrentUser().getUid()) .setValue(user)
– Frank van Puffelen
Nov 12 at 15:15












Actually I'm asking why storeUserCredentials() gets called. As the google account is already registered, storeUserCredentials() should not be called again. Probably I am missing something, but I cant figure it out.
– N.K.T
Nov 12 at 15:20




Actually I'm asking why storeUserCredentials() gets called. As the google account is already registered, storeUserCredentials() should not be called again. Probably I am missing something, but I cant figure it out.
– N.K.T
Nov 12 at 15:20




2




2




You're calling signInWithCredential, which successfully completes then the user is signed in.
– Frank van Puffelen
Nov 12 at 15:29






You're calling signInWithCredential, which successfully completes then the user is signed in.
– Frank van Puffelen
Nov 12 at 15:29














The method is called because signInWithCredential is triggered always, not only the first time the user logs in the app
– bra_racing
Nov 12 at 15:32




The method is called because signInWithCredential is triggered always, not only the first time the user logs in the app
– bra_racing
Nov 12 at 15:32












1 Answer
1






active

oldest

votes


















1














When you call signInWithCredential the SDK returns a task that successfully completes when the user is signed in. Since you call storeUserCredentials on that condition, it will get called each time the user is signed in. This is actually a quite normal flow.



If you want to detect if this account is new to Firebase Authentication, you can check the creation time of the user. See Firebase Auth, how to know new user signed up, rather than existing user sign in?



If you want to detect whether you've already seen the user before, you'll want to check in the database before calling storeUserCredentials. But what you now have is a pretty idiomatic flow, since it for example ensures you store the up-to-date display name each time you ask the user to sign in.






share|improve this answer





















  • Solved. Thanks.
    – N.K.T
    Nov 12 at 17:09











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264133%2ffirebase-user-registration-with-google%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














When you call signInWithCredential the SDK returns a task that successfully completes when the user is signed in. Since you call storeUserCredentials on that condition, it will get called each time the user is signed in. This is actually a quite normal flow.



If you want to detect if this account is new to Firebase Authentication, you can check the creation time of the user. See Firebase Auth, how to know new user signed up, rather than existing user sign in?



If you want to detect whether you've already seen the user before, you'll want to check in the database before calling storeUserCredentials. But what you now have is a pretty idiomatic flow, since it for example ensures you store the up-to-date display name each time you ask the user to sign in.






share|improve this answer





















  • Solved. Thanks.
    – N.K.T
    Nov 12 at 17:09
















1














When you call signInWithCredential the SDK returns a task that successfully completes when the user is signed in. Since you call storeUserCredentials on that condition, it will get called each time the user is signed in. This is actually a quite normal flow.



If you want to detect if this account is new to Firebase Authentication, you can check the creation time of the user. See Firebase Auth, how to know new user signed up, rather than existing user sign in?



If you want to detect whether you've already seen the user before, you'll want to check in the database before calling storeUserCredentials. But what you now have is a pretty idiomatic flow, since it for example ensures you store the up-to-date display name each time you ask the user to sign in.






share|improve this answer





















  • Solved. Thanks.
    – N.K.T
    Nov 12 at 17:09














1












1








1






When you call signInWithCredential the SDK returns a task that successfully completes when the user is signed in. Since you call storeUserCredentials on that condition, it will get called each time the user is signed in. This is actually a quite normal flow.



If you want to detect if this account is new to Firebase Authentication, you can check the creation time of the user. See Firebase Auth, how to know new user signed up, rather than existing user sign in?



If you want to detect whether you've already seen the user before, you'll want to check in the database before calling storeUserCredentials. But what you now have is a pretty idiomatic flow, since it for example ensures you store the up-to-date display name each time you ask the user to sign in.






share|improve this answer












When you call signInWithCredential the SDK returns a task that successfully completes when the user is signed in. Since you call storeUserCredentials on that condition, it will get called each time the user is signed in. This is actually a quite normal flow.



If you want to detect if this account is new to Firebase Authentication, you can check the creation time of the user. See Firebase Auth, how to know new user signed up, rather than existing user sign in?



If you want to detect whether you've already seen the user before, you'll want to check in the database before calling storeUserCredentials. But what you now have is a pretty idiomatic flow, since it for example ensures you store the up-to-date display name each time you ask the user to sign in.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 15:33









Frank van Puffelen

226k27369396




226k27369396












  • Solved. Thanks.
    – N.K.T
    Nov 12 at 17:09


















  • Solved. Thanks.
    – N.K.T
    Nov 12 at 17:09
















Solved. Thanks.
– N.K.T
Nov 12 at 17:09




Solved. Thanks.
– N.K.T
Nov 12 at 17:09


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264133%2ffirebase-user-registration-with-google%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python