登录Firebase Android后获取Uid [英] Get Uid after Signing in Firebase Android

查看:38
本文介绍了登录Firebase Android后获取Uid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,用户成功登录后,可以根据自己的喜好注册并进行活动,而用户可以登录然后登录.我可以注册并登录.但是在登录后,当我无法从firebase auth获取当前用户时.并且我是否正确登录.我是android新手.所有帮助将不胜感激.第一个代码是loginactivity,第二个代码是我试图获取uid的地方.

In my app a User can sign up and than sign in, after successfully signing in he's taken to an activity with events and stuff according to his preferences. I am able to sign up and sign in. But after signing when i can't get the current user from firebase auth. and am i signing in properly. I am new to android. All help will be appreciated. The first code is loginactivity and the second one is where i am trying to get the uid.

public class LoginActivity extends AppCompatActivity {

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    private DatabaseReference mdatabase;
    private DatabaseReference fdatabase;
    FirebaseAuth auth = FirebaseAuth.getInstance();
    FirebaseUser user = auth.getCurrentUser();

    private EditText txtEmailLogin;
    private EditText txtPassLogin;
    private FirebaseAuth firebaseAuth;
    private FirebaseAuth.AuthStateListener mAuthlistener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        txtEmailLogin = findViewById(R.id.EmailEditText);
        txtPassLogin = findViewById(R.id.PassEditText);
        firebaseAuth = FirebaseAuth.getInstance();
        //controleert als er ingelogd moet worden of niet.
       /* if (firebaseAuth.getCurrentUser() != null)
        {
            Intent i = new Intent(LoginActivity.this, Home.class);
            i.putExtra("Email", firebaseAuth.getCurrentUser().getEmail());
            startActivity(i);
        }*/

    }
    public void btnregister_Click(View v) {
        Intent i = new Intent(LoginActivity.this,RegisterActivity.class);
        startActivity(i);
    }

   public void btnLogin_Click(View v){

        ProgressDialog progressDialog = ProgressDialog.show(LoginActivity.this,"Please Wait...", "Processing...", true);

        (firebaseAuth.signInWithEmailAndPassword(txtEmailLogin.getText().toString(), txtPassLogin.getText().toString()))
                .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

                if(task.isSuccessful()) {
                    Toast.makeText(LoginActivity.this ,"Login Succesfull" ,Toast.LENGTH_SHORT).show();
                    Intent i = new Intent(LoginActivity.this, Home.class);
                    startActivity(i);
                }
                else
                {
                    Log.e("Error", task.getException().toString());
                    Toast.makeText(LoginActivity.this, task.getException().getMessage(),Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}



public class Home extends AppCompatActivity {

    FirebaseAuth mAuth;
    FirebaseAuth.AuthStateListener mAuthListener;
    TextView txtUserNaam;
    String uid,UserName;
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference mdatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        mAuth=FirebaseAuth.getInstance();
        mAuthListener=new FirebaseAuth.AuthStateListener() {

            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

                if (mAuth.getCurrentUser().getUid() == null) {

                    //Your action here
                    Intent i = new Intent(Home.this, LoginActivity.class);
                    startActivity(i);

                } else {

                    txtUserNaam = findViewById(R.id.txtusernaam);

                    mdatabase.child("USERS").child(firebaseAuth.getCurrentUser().getUid()).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            UserName = dataSnapshot.child("UserName").getValue(String.class);
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {    
                        }
                    });
                    txtUserNaam.setText(UserName);
                }

            }
        };
    }
}

推荐答案

而不是声明

FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseUser user = auth.getCurrentUser();

将此添加到您的OnCreate

Add this into your OnCreate

FirebaseUser mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
    if (mCurrentUser != null) {
        uid = mCurrentUser.getUid();

然后,如果需要,您可以将uid声明为 public static String uid ,这样您就不必重写代码,而只需使用变量

Then if you want you can declare your uid as public static String uid so that you would not have to rewrite the code and could just use the variable

uid

这篇关于登录Firebase Android后获取Uid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆