如何保持用户状态登录firebase Oauth android

时间:2017-06-29 12:20:20

标签: android firebase oauth firebase-authentication

我是初级编码员并面临问题:/ 我正在使用Firebase OAuth实现用户登录注册....

我的登录活动代码是....

    public class UserLogin extends AppCompatActivity {
    Button SignInBtn;
    EditText EmailField, PasswordField;
    TextView notReg;
    FirebaseAuth mAuth;
    FirebaseAuth.AuthStateListener mAuthListner;
    String UserEmail, UserPass;


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(mAuthListner);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mAuthListner != null) {
            mAuth.removeAuthStateListener(mAuthListner);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_login);
        Initialization(); // views initialized
        mAuth = FirebaseAuth.getInstance();
        mAuthListnerSetup();


        SignInBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserEmail = EmailField.getText().toString();
                if (TextUtils.isEmpty(UserEmail)) {
                    EmailField.setError("Enter Email.");
                }
                UserPass = PasswordField.getText().toString();
                if (TextUtils.isEmpty(UserPass)) {
                    PasswordField.setError("Enter Password");
                }
                SignIn(UserEmail, UserPass);
            }
        });

        notReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(UserLogin.this, MainActivity.class));
            }
        });

    } //onCreate Method

    private void mAuthListnerSetup() {
        mAuthListner = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
            }
        };
    }

    private void Initialization() {
        EmailField = (EditText) findViewById(R.id.userSigninEmail);
        PasswordField = (EditText) findViewById(R.id.userSigninPass);
        SignInBtn = (Button) findViewById(R.id.SigninBtn);
        notReg = (TextView) findViewById(R.id.notRegUser);
    }

    private void SignIn(String email, String pass) {
        mAuth.signInWithEmailAndPassword(email, pass).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (!task.isSuccessful()) {
                    Toast.makeText(UserLogin.this, "Failed...", Toast.LENGTH_SHORT).show();
                } else {
                    startActivity(new Intent(UserLogin.this, all.class));
                }
            }
        });
    }

}

当用户成功登录时,新活动开始,我想从下一个活动访问用户详细信息....:/ 在此先感谢希望我会得到帮助:)

1 个答案:

答案 0 :(得分:1)

您可以使用此代码:

FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
String uid = firebaseUser.getUid();
String userName = firebaseUser.getDisplayName();
String emailAddress = firebaseUser.getEmail();
//and so on for the others details

要在活动之间使用数据,您可以使用Intent,如下所示:

//FirstActivity
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("uid", uid);
intent.putExtra("userName", userName);
intent.putExtra("emailAddress", emailAddress);
startActivity(intent);

要获取数据,请使用以下代码:

//SecondActivity
String uid = (String) getIntent().getExtras().get("uid");
String userName = (String) getIntent().getExtras().get("userName");
String emailAddress = (String) getIntent().getExtras().get("emailAddress");

希望它有所帮助。