验证用户是新用户还是已经存在的Android Studio

时间:2018-08-21 16:46:22

标签: android firebase firebase-authentication

我正在开发一个应用程序,并且使用firebase进行用户登录身份验证,并且一切正常。最近,我想到了该应用程序的一个新主意,我想检查用户是否存在或是否是第一次。如果是第一次用户进入新活动,如果用户存在,则进入家庭活动。我尝试了文档,但我不太了解。有人可以帮助我提供代码示例或更好的解释。

这是登录代码

private void user_login(String email, String password) {

    if (email.isEmpty()){
      emailtx.setError("email is empty");
      emailtx.requestFocus();
    }
    if (password.isEmpty()){
        passwordtx.setError("password is empty");
        passwordtx.requestFocus();
    }
    if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
        emailtx.setError("Please enter a valid email");
        emailtx.requestFocus();
        return;
    }
    if (password.length()< 6){
        passwordtx.setError("password is short");
        passwordtx.requestFocus();
    }



    mAuth.signInWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        FirebaseUser current_user = FirebaseAuth.getInstance().getCurrentUser();

                        String user_id = current_user.getUid();

                        String device_token = FirebaseInstanceId.getInstance().getToken();

                        mDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(user_id);




                        HashMap<String, String> userMap = new HashMap<>();
                        userMap.put("image", "https://firebasestorage.googleapis.com/v0/b/bookshare-8e018.appspot.com/o/male.png?alt=media&token=60cbb24c-b9f9-4724-b8bf-69a76386fcca");
                        userMap.put("thumb_image", "default");
                        userMap.put("device_token", device_token);
                        userMap.put("home", "earth");
                        userMap.put("work", "earth");
                        userMap.put("others", "earth");

                        mDatabase.setValue(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {

                                if(task.isSuccessful()){


                                    Intent mainIntent = new Intent(login.this, selectestado.class);
                                    mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                    startActivity(mainIntent);
                                    finish();

                                }

                            }
                        });
                    } 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();
                    }

                    // ...
                }
            });

1 个答案:

答案 0 :(得分:0)

在mDatabase下面,您可以检查用户是否存在

mDatabase.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
    if(dataSnapshot.exists()){

                        HashMap<String, String> userMap = new HashMap<>();
                        userMap.put("image", "https://firebasestorage.googleapis.com/v0/b/bookshare-8e018.appspot.com/o/male.png?alt=media&token=60cbb24c-b9f9-4724-b8bf-69a76386fcca");
                        userMap.put("thumb_image", "default");
                        userMap.put("device_token", device_token);
                        userMap.put("home", "earth");
                        userMap.put("work", "earth");
                        userMap.put("others", "earth");

                        mDatabase.setValue(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {

                                if(task.isSuccessful()){


                                    Intent mainIntent = new Intent(login.this, selectestado.class);
                                    mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                    startActivity(mainIntent);
                                    finish();

                                }

                            }
                        });
                    } 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();
                    }

       }
  }else{ 
     //User does not exists
       }

  @Override
  public void onCancelled(DatabaseError databaseError) {
    System.out.println("The read failed: " + databaseError.getCode());
  }
});