我正在使用Firebase身份验证在Android中创建一个简单的身份验证应用。直到现在我成功签署了用户,但问题是用户仍然登录,我找不到签署他的方法。
这是我的MainActivity.java代码
public class MainActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//tracking the sign in and singn out operations
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener(){
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user!=null){
System.out.println("User logged in");
}
else{
System.out.println("User not logged in");
}
}
};
}
public void onStart(){
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
public void onStop(){
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
public void buttonClicked(View view){
EditText editemail = (EditText) findViewById(R.id.email);
EditText editpass = (EditText) findViewById(R.id.password);
String email = editemail.getText().toString();
String password = editpass.getText().toString();
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
// Log.d(TAG, "signInWithEmail:onComplete:" + task.isSuccessful());
Toast.makeText(MainActivity.this, "Authentication Success.",
Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this,Success.class));
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
// Log.w(TAG, "signInWithEmail", task.getException());
Toast.makeText(MainActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
}
}
答案 0 :(得分:19)
使用此代码FirebaseAuth.getInstance().signOut();
答案 1 :(得分:13)
您可以直接调用此
FirebaseAuth.getInstance().signOut();
如果您想在退出后执行某些操作,请使用此操作。
public void onClick(View v) {
if (v.getId() == R.id.sign_out) {
AuthUI.getInstance()
.signOut(this)
.addOnCompleteListener(new OnCompleteListener<Void>() {
public void onComplete(@NonNull Task<Void> task) {
// user is now signed out
startActivity(new Intent(MyActivity.this, SignInActivity.class));
finish();
}
});
}
}
答案 2 :(得分:5)
试试这个
FirebaseAuth fAuth = FirebaseAuth.getInstance();
fAuth.signOut();
答案 3 :(得分:5)
如果您使用firebaseAuthUI,则推荐的方法是
public void onClick(View v) {
if (v.getId() == R.id.sign_out) {
AuthUI.getInstance()
.signOut(this)
.addOnCompleteListener(new OnCompleteListener<Void>() {
public void onComplete(@NonNull Task<Void> task) {
// user is now signed out
startActivity(new Intent(MyActivity.this, SignInActivity.class));
finish();
}
});
}
}
根据firebaseAuthUI github Guide。
答案 4 :(得分:4)
这可以通过使用AuthStateListener
来解决//Declaration and defination
private FirebaseAuth firebaseAuth;
FirebaseAuth.AuthStateListener authStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() == null){
//Do anything here which needs to be done after signout is complete
signOutComplete();
}
else {
}
}
};
//Init and attach
firebaseAuth = FirebaseAuth.getInstance();
firebaseAuth.addAuthStateListener(authStateListener);
//Call signOut()
firebaseAuth.signOut();
答案 5 :(得分:3)
Firebase身份验证提供了注销方法。
FirebaseAuth.getInstance().signOut();
答案 6 :(得分:1)
这个只是从当前的应用程序中签出。
如果您正在使用片段,请执行此操作
AuthUI.getInstance().signOut(getActivity());
如果您正在使用某项活动,请执行此操作
AuthUI.getInstance().signOut(getApplicationContext);
<强>欢呼声强>!
答案 7 :(得分:1)
FirebaseAuth.getInstance()。signOut();
答案 8 :(得分:1)
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
firebaseAuth.getInstance().signOut();
Intent intent = new Intent(SettingsActivity.this,SignInActivity.class);
startActivity(intent);
}
});
答案 9 :(得分:0)
有多种方法可以退出用户:
<强> 1。 FirebaseUI: Refarence
添加附加费:
dependencies {
implementation 'com.firebaseui:firebase-ui-auth:4.0.0'
}
然后:
public void onClick(View v) {
if (v.getId() == R.id.sign_out) {
AuthUI.getInstance()
.signOut(this)
.addOnCompleteListener(new OnCompleteListener<Void>() {
public void onComplete(@NonNull Task<Void> task) {
// user is now signed out
startActivity(new Intent(MyActivity.this, SignInActivity.class));
finish();
}
});
}
}
<强> 2。 Kotlin: Referance
使用Android默认身份验证依赖项,例如: com.google.firebase:firebase-auth:16.0.1
firebase.auth().signOut().then(function() {
// Sign-out successful.
}).catch(function(error) {
// An error happened.
});
第3。默认使用java:
使用Android默认身份验证依赖项,例如: com.google.firebase:firebase-auth:16.0.1
FirebaseAuth mAuth = FirebaseAuth.getInstance();
try {
mAuth.signOut();
Toast.makeText(this, "User Sign out!", Toast.LENGTH_SHORT).show();
}catch (Exception e) {
Log.e(TAG, "onClick: Exception "+e.getMessage(),e );
}
答案 10 :(得分:0)
使用这个`
findViewById(R.id.signout).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
Intent intent = new Intent(currentActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);//makesure user cant go back
startActivity(intent);
}
});`
答案 11 :(得分:0)
FirebaseAuth.getInstance().signOut();
然后,检测登录状态:
private FirebaseAuth mAuth = FirebaseAuth.getInstance()
public static boolean mAuthSignIn() {
if (mAuth != null) {
FirebaseUser user = mAuth.getCurrentUser();
return user != null;
}
return false;
}
答案 12 :(得分:0)
要完全注销,我建议:
mFirebaseAuth.signOut();
Auth.GoogleSignInApi.signOut(mGoogleApiClient);
答案 13 :(得分:-3)
试试这个,它对我有用。
FirebaseAuth.getInstance()
.signOut(this)
.addOnCompleteListener(new OnCompleteListener<Void>() {
public void onComplete(@NonNull Task<Void> task) {
// user is now signed out
startActivity(new Intent(YOUR CURRENT ACTIVITY, ACTIVITY IN WHICH YOU WANT TO MOVE));
finish();
}
});