Check if user is authenticated for the first time in Firebase Google Authentication in Android

后端 未结 8 1435
一向
一向 2020-11-30 06:16

I am using Firebase Authentication in an Android application, and I am using Google account authentication as an option to sign in to the application.

How can I know

8条回答
  •  独厮守ぢ
    2020-11-30 06:54

    According to the new version of Firebase auth (16.0.1) The AuthResult class has a member function which results true or false (is the user is new). Assuming "credential" is defined in the scope(it is the google credential ). An example is shown below: `

    private FirebaseAuth mAuth;
    
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();
    
    mAuth = FirebaseAuth.getInstance();
    Task task = GoogleSignIn.getSignedInAccountFromIntent(data);
    GoogleSignInAccount acct = task.getResult(ApiException.class);
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    
    mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener() {
                    @Override
                    public void onComplete(@NonNull Task task) {
                        if (task.isSuccessful()) {
                            Log.d(TAG, "linkWithCredential:success");
    
    
                            boolean newuser = task.getResult().getAdditionalUserInfo().isNewUser();
    
    
    
                            if(newuser){
    
                                 //Do Stuffs for new user
    
                             }else{
    
                                //Continue with Sign up 
                            }
    
                        } else {
    
                            Toast.makeText(MyClass.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
    
                        }
    
    
                });
    

    Thanks to firebase:)

提交回复
热议问题