本文介绍了如何使用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 ?

更新:我将google登录功能与Firebase身份验证配合使用

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';
}

我的登录页面如下:

    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 并调用 await FirebaseAuth.instance.signOut();。 ,以便在用户注销后调用 runApp

Firebase auth's signOut method is asynchronous. You should make your _signOut method async and call await FirebaseAuth.instance.signOut(); so that the call to runApp occurs after the user is signed out.

您如果要让 signIn 向用户显示身份验证对话框,则在注销时也应调用 _googleSignIn.signOut()并自动重用当前的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中注销用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 08:05