我想知道即使用户未经过身份验证,我是否可以从Firebase数据库获取数据。 我没有理由这样做。其中之一是用户首次注册并将其数据保存在FirebaseDatabase上。一旦他退出并想再次登录,我想跳过MainActivity的注册活动。
以下是代码示例
public class FireBaseLoginActivity extends AppCompatActivity {
private FirebaseUser currentUser;
private DatabaseReference refToUsers;
private static final int RC_SIGN_IN = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fire_base_login);
currentUser = FirebaseAuth.getInstance().getCurrentUser();
refToUsers =
FirebaseDatabase.getInstance().getReference().child(Params.USERS);
startSignIn();
}
}
public void startSignIn(){
List<AuthUI.IdpConfig> providers = Arrays.asList(
new AuthUI.IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER)
.setPermissions(Arrays.asList(Scopes.PROFILE,Scopes.EMAIL)).build(),
new AuthUI.IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build());
//new AuthUI.IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER).build());
Intent intent = AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers).build();
startActivityForResult(intent,RC_SIGN_IN);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RC_SIGN_IN){
IdpResponse idpResponse = IdpResponse.fromResultIntent(data);
if(idpResponse != null && resultCode == RESULT_OK){
// Log.d("currentuser",currentUser.getUid().toString());
//check if the user is exists by userUID. if he is it jumps to main activity
refToUsers.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChild(currentUser.getUid())){
Intent mainActivity = new Intent(getApplicationContext(), MainActivity.class);
mainActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mainActivity);
}else{
//if the user is not exists it will move to register activity
Intent registerActivity = new Intent(getApplicationContext(), RegisterActivity.class);
registerActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(registerActivity);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}else if (idpResponse.getErrorCode() == ErrorCodes.NO_NETWORK) {
errorToast("No internet connection");
}else if (idpResponse.getErrorCode() == ErrorCodes.UNKNOWN_ERROR) {
errorToast("Unknown Error");
}
}
}
在这里,我使用FirebaseUI登录。 refToUsers
是对用户列表的引用,我检查userUID是否存在。如果是,它将跳过注册活动。
如果没有,它会跳转到RegisterActivity
。
由于某种原因没有任何反应它只是跳过整个refToUser
事件监听器。
我不知道是什么原因。
也许你们有更好的解决方案给我?
我还要在FirebaseDatabase中显示存储在那里的RecyclerView中的数据。如果用户已登录,则应该生效