如何使用 Firebase 身份验证在 Flutter 中注销用户 [英] How to Signout a user in Flutter with Firebase authentication

查看:18
本文介绍了如何使用 Firebase 身份验证在 Flutter 中注销用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从我的应用中注销当前用户时遇到问题

I have a problem signing out the current user from my app

我使用的方法如下:

....
onPressed:_signOut
//jump to function


  void _signOut() {
  FirebaseAuth.instance.signOut();
  FirebaseUser user = FirebaseAuth.instance.currentUser;
  //print('$user');
  runApp(
      new MaterialApp(
        home: new LoginPage(),
      )

  );
}

所以现在当我按下按钮时,它应该让用户退出并将他们重定向到他们必须再次登录的主页,但是,重定向发生了,但用户数据仍然会被保存,所以当我按下按钮时再次它会自动使用最后一个帐户再次登录.如何删除用户数据,以便应用在他们每次注销后尝试登录时询问他们的凭据?

So now when I press the button it should sign the user out and redirect them to the home page which they will have to login again, however, the redirecting happens but the user data would still be saved so when I press the button again it automatically sign in again with the last account. How can I remove user data so the app asks about their credentials each time they try to login after a logout ?

我觉得我在页面之间的链接以及它们的行为如何相应地变化中遗漏了一些东西,但它是什么?

I feel I am missing something in the linkage between pages and how their behavior change accordingly, but what is it ?

更新:我使用带有 firebase 身份验证的 google 登录功能

Update: I use google sign in function with firebase authentication

   Future<String> _testSignInWithGoogle() async {
  final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
  final GoogleSignInAuthentication googleAuth =
  await googleUser.authentication;
  final FirebaseUser user = await _auth.signInWithGoogle(
    accessToken: googleAuth.accessToken,
    idToken: googleAuth.idToken,
  );
  assert(user.email != null);
  assert(user.displayName != null);
  assert(!user.isAnonymous);
  assert(await user.getToken() != null);
return 'signInWithGoogle succeeded: $user';
}

我的登录页面如下所示:

My login page looks like this:

    class LoginPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    return new Scaffold(
        appBar: new AppBar(
          title: new Text("Login"), backgroundColor: Colors.blue,),
        body: new Container(
            child: new Center(
                child: new Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    new IconButton(
                      icon: new Icon(Icons.account_box, color: Colors.red),
                      onPressed:  _signIn,
                      iconSize: 80.0,),
                    new Text("Google Signin")
                  ],
                )
            )
        )
    );
  }
}

更新:将 _signOut() 方法更改为异步如下:

Update: Changed _signOut() method to be async as follows:

    Future <LoginPage> _signOut()  async{
    await FirebaseAuth.instance.signOut();

    return new LoginPage();
}

现在,当我按下注销时,它不会将我重定向到 LoginPagae,也不会将用户注销.

Now when I press on signout, it does not redirect me to the LoginPagae nor does it sign the user out.

推荐答案

Firebase auth 的 signOut 方法是异步的.你应该让你的 _signOut 方法 async.

Firebase auth's signOut method is asynchronous. You should make your _signOut method async.

Future<void> _signOut() async {
  await FirebaseAuth.instance.signOut();
}

以便在用户注销后调用 runApp.

so that the call to runApp occurs after the user is signed out.

如果您希望 signIn 向用户显示身份验证对话框,而不是静默并自动重新使用当前 Google 用户.

You should also call _googleSignIn.signOut() when logging out if you want signIn to present the user with an authentication dialog instead of silently and automatically re-using the current Google user.

这篇关于如何使用 Firebase 身份验证在 Flutter 中注销用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆