本文介绍了Firebase将用户身份验证链接到其他活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个用户通过Firebase使用一项活动登录我的应用程序,我可以在下一个活动中访问用户的详细信息。例如:
登录类:
其他类,您可以检测到用户的登录状态,而不需要他们再次登录。authlistener = new FirebaseAuth .AuthStateListener(){
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth){
FirebaseUser user = firebaseAuth.getCurrentUser();
if(user!= null){
//用户已登录
} else {
//用户退出
}
// ...
}
};
FirebaseAuth.getInstance()。addAuthStateListener(authListener);
I have say a user who signs in to my app using one activity via Firebase, can I access the user's details in the next activity. Eg:
Login class:
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(); } } }); } }Logged in Activity:
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); } }Say I want to change the password of the user in the loggedinClass. Do I have to uses the intent from the "login.class" and then use the sign in method in the loggedin class rather than the login.class.
but what if I had multiple activities, this would get tedious, signing in to every activity?
解决方案If you put an AuthStateListener in the other classes, you can detect the sign-in state of the user without requiring them to sign in again.
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);
这篇关于Firebase将用户身份验证链接到其他活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!