Javascript 如何在 Firebase 3.0 中注销用户?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37376708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:15:14  来源:igfitidea点击:

How do I sign out users in Firebase 3.0?

javascriptfirebasefirebase-authentication

提问by hellogoodnight

According to documentation, I force a user to sign out with the method signOut().

根据文档,我强制用户使用该方法注销signOut()

This is what I have tried:

这是我尝试过的:

var rootRef = firebase.database().ref();
var loggedInUser = firebase.auth();

1. firebase.signOut(); 
2. loggedInUser.signOut(); 
3. rootRef.signOut();
4. signOut();
5. firebase.auth.signOut();

I get ... is not a functionfor every one of the five above. I know there is no issue with my reference to the new Firebase, since firebase.database().ref();and firebase.auth();does not throw error. I have also migrated the app in the console.

我得到... is not a function了上面五个中的每一个。我知道我对新 Firebase 的引用没有问题,因为firebase.database().ref();并且firebase.auth();不会引发错误。我还迁移了控制台中的应用程序。

回答by Frank van Puffelen

In JavaScript you can sign out the user with:

在 JavaScript 中,您可以使用以下命令注销用户:

firebase.auth().signOut().then(function() {
  console.log('Signed Out');
}, function(error) {
  console.error('Sign Out Error', error);
});

回答by Thien Nhan Nguyen

firebase.auth().signOut()

simply it works for me!

简直对我有用!

回答by Hasib Akter

There have several way to sign out user:

有几种方法可以注销用户:

1. FirebaseUI:Refarence

1. FirebaseUI:参考

Add depenencies:

添加依赖:

dependencies {
    implementation 'com.firebaseui:firebase-ui-auth:4.0.0'
}

Then:

然后:

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

2. Kotlin:参考

Use Android default Authentication dependency, ex:com.google.firebase:firebase-auth:16.0.1

使用 Android 默认身份验证依赖项,例如:com.google.firebase:firebase-auth:16.0.1

firebase.auth().signOut().then(function() {
  // Sign-out successful.
}).catch(function(error) {
  // An error happened.
});


3. Default with java:

3.默认使用java:

Use Android default Authentication dependency, ex:com.google.firebase:firebase-auth:16.0.1

使用 Android 默认身份验证依赖项,例如:com.google.firebase:firebase-auth:16.0.1

FirebaseUser user = mAuth.getCurrentUser();
if (user != null){
    mAuth.signOut();
    Toast.makeText(this, user.getEmail()+ " Sign out!", Toast.LENGTH_SHORT).show();
}else{
    Toast.makeText(this, "You aren't login Yet!", Toast.LENGTH_SHORT).show();
}

回答by afrozopsy dev

Extending the answer of @Frank van Puffelen, this code works like a charm, but promise rejection handling inside then()function as a second parameter is a bad practice.

扩展@Frank van Puffelen 的答案,这段代码就像一个魅力,但承诺在then()函数内部作为第二个参数进行拒绝处理是一种不好的做法。

Rather add a .catch(e)block.

而是添加一个.catch(e)块。

Because, if error happens at signout()function then would be handled but if error happens at .then()block then it wont be handled.

因为,如果错误发生在signout()函数上,那么会被处理,但如果错误发生在.then()块上,那么它就不会被处理。

Better code would be,

更好的代码是,

firebase.auth().signOut()
.then(() => {
  console.log('Signed Out');
})
.catch(e=>{
 console.error('Sign Out Error', e);
});

回答by Thib

I don't know if I correctly understood, but if you want to sign out every user signed in: That's not possible since the code is running on the clientand the auth state refers to the client running it.

我不知道我的理解是否正确,但是如果您想注销每个登录的用户:这是不可能的,因为代码在客户端上运行而 auth 状态是指运行它的客户端。

You can't access every client connected to the firebase auth service since it would mean running code on the server side.

您无法访问连接到 Firebase 身份验证服务的每个客户端,因为这意味着在服务器端运行代码。

However there's an option to specify the duration of a session, which is the rememberparameter in the auth section.

但是,有一个选项可以指定会话的持续时间,这是auth 部分中的记住参数。