Firebase将用户身份验证链接到另一个活动

时间:2016-10-12 07:23:18

标签: android firebase firebase-authentication

我已经说过通过Firebase使用一项活动登录我的应用的用户,我可以在下一个活动中访问用户的详细信息。例如:

登录课程:

public class LoginActivity extends AppCompatActivity {

    EditText editText;
    EditText editText2;
    Button button;
    public FirebaseAuth Auth = FirebaseAuth.getInstance();
    public FirebaseAuth.AuthStateListener authlistener;
    String email, pass;
    ProgressBar bar;

    public View.OnClickListener buttonListener = new View.OnClickListener() {
        public void onClick (View view){
            email = editText.getText().toString().trim();
            pass = editText2.getText().toString().trim();
            logInUser();

        }};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText)findViewById(R.id.editText);
        editText2 = (EditText)findViewById(R.id.editText2);
        button = (Button)findViewById(R.id.button2);
        Auth = FirebaseAuth.getInstance();
        button.setOnClickListener(buttonListener);
        bar = (ProgressBar)findViewById(R.id.progressBar2);
        bar.setVisibility(View.INVISIBLE);

        authlistener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    // User is signed in

                } else {
                    // User is signed out

                }
                // ...
            }
        };
        // ...
    }

    @Override
    public void onStart() {
        super.onStart();
        Auth.addAuthStateListener(authlistener);
    }

    @Override
    public void onStop() {
        super.onStop();
        if (authlistener != null) {
            Auth.removeAuthStateListener(authlistener);
        }
    }

    public void logInUser(){
        bar.setVisibility(View.VISIBLE);

        Auth.signInWithEmailAndPassword(email, pass)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {

                        if(task.isSuccessful()) {

                            Intent loggedInActivity = new Intent(getApplicationContext(), Loggedin.class);
                            startActivity(loggedInActivity);

                        }
                        else{

                            Toast.makeText(getApplicationContext(), "There was an error, try again", Toast.LENGTH_SHORT).show();
                        }


                    }
                });


    }
}

登录活动:

public class Loggedin extends AppCompatActivity {

    Button changeMail, changePass;
    String oldPassword;
    String newPassword;
    EditText oldpass, newpass;
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    public FirebaseAuth Auth = FirebaseAuth.getInstance();

    public void updatePassword(){
        oldPassword = oldpass.getText().toString();
        newPassword = newpass.getText().toString();



    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loggedin);
        changeMail = (Button)findViewById(R.id.changeMail);
        changePass = (Button)findViewById(R.id.changePass);
        oldpass = (EditText)findViewById(R.id.oldPass);
        newpass = (EditText)findViewById(R.id.newPass);
        oldpass.setVisibility(View.INVISIBLE);
        newpass.setVisibility(View.VISIBLE);

    }
}

我想要更改loggedinClass中用户的密码。我是否必须使用&#34; login.class&#34;的意图?然后使用sign in类中的loggedin方法而不是login.class。

但是,如果我有多项活动,这会变得乏味,登录每项活动会怎样?

2 个答案:

答案 0 :(得分:1)

如果您在其他课程中添加AuthStateListener,则可以检测用户的登录状态,而无需再次登录。

authlistener = new FirebaseAuth.AuthStateListener() {
   @Override
   public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
       FirebaseUser user = firebaseAuth.getCurrentUser();
       if (user != null) {
            // User is signed in

        } else {
            // User is signed out

        }
        // ...
    }
};
FirebaseAuth.getInstance().addAuthStateListener(authListener);

答案 1 :(得分:1)

您不需要Firebase的监听器......它是一个单身人士,可在您的整个活动中使用。只需将其添加到您的基本活动

即可
public FirebaseUser getFirebaseUser() {
    return FirebaseAuth.getInstance().getCurrentUser();
}

或使用if(FirebaseAuth.getInstance()。getCurrentUser()!= null){...}您需要的地方