How can I validate user is already signed up?

ⅰ亾dé卋堺 提交于 2020-05-17 08:28:27

问题


How can I validate user is already signed up?

private void firebaseAuthWithGoogle(final GoogleSignInAccount acct, final GoogleSignInResult result) {

    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()) {

       //HOW CAN I validate user is already signup or not
                        if (Singup == true) {
                            did not create account.
                        } else {
                            create account;
                        }
                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "signInWithCredential:failure", task.getException());
                        Toast.makeText(Login.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                        //        updateUI(null);
                    }
                }
            });
}

This is creating account when user already exists or not every time.


回答1:


You will need to have an instance of FirebaseAuth. Then within if (task.isSuccessful()) {...} block, get an instance of FirebaseUser using FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();.

After which you can replace

if (Singup == true) { // did not create account. } else { // create account; }

with

if (firebaseUser == null) { // did not create account. } else { // create account; }

More details can be found on lines 137 and 197 sample and here (Firebase google sigin doc)



来源:https://stackoverflow.com/questions/46800611/how-can-i-validate-user-is-already-signed-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!